CustomizeStoreWithBlocks.php
2 years ago
CustomizingProductCatalog.php
1 year ago
EUVATNumber.php
2 years ago
EditProductsOnTheMove.php
2 years ago
EmailImprovements.php
1 year ago
FirstProduct.php
1 year ago
GivingFeedbackNotes.php
3 years ago
InstallJPAndWCSPlugins.php
4 weeks ago
LaunchChecklist.php
2 years ago
MagentoMigration.php
2 years ago
ManageOrdersOnTheGo.php
2 years ago
MarketingJetpack.php
4 weeks ago
MigrateFromShopify.php
2 years ago
MobileApp.php
2 years ago
NewSalesRecord.php
3 years ago
NoteActionForbiddenException.php
4 weeks ago
OnboardingPayments.php
2 years ago
OnlineClothingStore.php
2 years ago
OrderMilestones.php
2 years ago
PaymentsMoreInfoNeeded.php
5 months ago
PaymentsRemindMeLater.php
5 months ago
PerformanceOnMobile.php
2 years ago
PersonalizeStore.php
3 years ago
RealTimeOrderAlerts.php
2 years ago
ScheduledUpdatesPromotion.php
5 months ago
SellingOnlineCourses.php
2 years ago
TrackingOptIn.php
1 year ago
UnsecuredReportFiles.php
2 years ago
WooCommercePayments.php
2 years ago
WooCommerceSubscriptions.php
2 years ago
WooSubscriptionsNotes.php
1 year ago
InstallJPAndWCSPlugins.php
166 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin Add Install Jetpack and WooCommerce Shipping & Tax Plugin Note Provider. |
| 4 | * |
| 5 | * Adds a note to the merchant's inbox prompting them to install the Jetpack |
| 6 | * and WooCommerce Shipping & Tax plugins after it fails to install during |
| 7 | * WooCommerce setup. |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\Admin\Notes; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | use Automattic\WooCommerce\Admin\Notes\Note; |
| 15 | use Automattic\WooCommerce\Admin\Notes\Notes; |
| 16 | use Automattic\WooCommerce\Admin\Notes\NoteTraits; |
| 17 | use Automattic\WooCommerce\Admin\PluginsHelper; |
| 18 | |
| 19 | /** |
| 20 | * Install_JP_And_WCS_Plugins |
| 21 | */ |
| 22 | class InstallJPAndWCSPlugins { |
| 23 | /** |
| 24 | * Note traits. |
| 25 | */ |
| 26 | use NoteTraits; |
| 27 | |
| 28 | /** |
| 29 | * Name of the note for use in the database. |
| 30 | */ |
| 31 | const NOTE_NAME = 'wc-admin-install-jp-and-wcs-plugins'; |
| 32 | |
| 33 | /** |
| 34 | * Constructor. |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | add_action( 'woocommerce_note_action_install-jp-and-wcs-plugins', array( $this, 'install_jp_and_wcs_plugins' ) ); |
| 38 | add_action( 'activated_plugin', array( $this, 'action_note' ) ); |
| 39 | add_action( 'woocommerce_plugins_install_api_error', array( $this, 'on_install_error' ) ); |
| 40 | add_action( 'woocommerce_plugins_install_error', array( $this, 'on_install_error' ) ); |
| 41 | add_action( 'woocommerce_plugins_activate_error', array( $this, 'on_install_error' ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the note. |
| 46 | * |
| 47 | * @return Note |
| 48 | */ |
| 49 | public static function get_note() { |
| 50 | $content = __( 'We noticed that there was a problem during the Jetpack and WooCommerce Shipping & Tax install. Please try again and enjoy all the advantages of having the plugins connected to your store! Sorry for the inconvenience. The "Jetpack" and "WooCommerce Shipping & Tax" plugins will be installed & activated for free.', 'woocommerce' ); |
| 51 | |
| 52 | $note = new Note(); |
| 53 | $note->set_title( __( 'Uh oh... There was a problem during the Jetpack and WooCommerce Shipping & Tax install. Please try again.', 'woocommerce' ) ); |
| 54 | $note->set_content( $content ); |
| 55 | $note->set_content_data( (object) array() ); |
| 56 | $note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); |
| 57 | $note->set_name( self::NOTE_NAME ); |
| 58 | $note->set_source( 'woocommerce-admin' ); |
| 59 | $note->add_action( |
| 60 | 'install-jp-and-wcs-plugins', |
| 61 | __( 'Install plugins', 'woocommerce' ), |
| 62 | false, |
| 63 | Note::E_WC_ADMIN_NOTE_ACTIONED |
| 64 | ); |
| 65 | return $note; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Action the Install Jetpack and WooCommerce Shipping & Tax note, if any exists, |
| 70 | * and as long as both the Jetpack and WooCommerce Shipping & Tax plugins have been |
| 71 | * activated. |
| 72 | */ |
| 73 | public static function action_note() { |
| 74 | // Make sure that both plugins are active before actioning the note. |
| 75 | $active_plugin_slugs = PluginsHelper::get_active_plugin_slugs(); |
| 76 | $jp_active = in_array( 'jetpack', $active_plugin_slugs, true ); |
| 77 | $wcs_active = in_array( 'woocommerce-services', $active_plugin_slugs, true ); |
| 78 | |
| 79 | if ( ! $jp_active || ! $wcs_active ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Action any notes with a matching name. |
| 84 | $data_store = Notes::load_data_store(); |
| 85 | $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); |
| 86 | |
| 87 | foreach ( $note_ids as $note_id ) { |
| 88 | $note = Notes::get_note( $note_id ); |
| 89 | |
| 90 | if ( $note ) { |
| 91 | $note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED ); |
| 92 | $note->save(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Install the Jetpack and WooCommerce Shipping & Tax plugins in response to the action |
| 99 | * being clicked in the admin note. |
| 100 | * |
| 101 | * @param Note $note The note being actioned. |
| 102 | * |
| 103 | * @throws NoteActionForbiddenException When the current user lacks the `install_plugins` capability. |
| 104 | */ |
| 105 | public function install_jp_and_wcs_plugins( $note ) { |
| 106 | if ( self::NOTE_NAME !== $note->get_name() ) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // The route-level permission check on `POST /wc-analytics/admin/notes/.../action/...` |
| 111 | // only requires `manage_woocommerce`, which `shop_manager` satisfies. Plugin install |
| 112 | // requires the dedicated `install_plugins` capability — gate per-handler. |
| 113 | // |
| 114 | // Throwing (rather than returning silently) prevents `Notes::trigger_note_action()` |
| 115 | // from persisting the action's `E_WC_ADMIN_NOTE_ACTIONED` status: the exception |
| 116 | // aborts the dispatch before the status-saving `$note->save()` inside |
| 117 | // `Notes::trigger_note_action()` runs, so the note stays actionable in the inbox. |
| 118 | // (The REST controller has already saved `is_read = true` at this point — that |
| 119 | // earlier save is intentional and unaffected by the cap check.) |
| 120 | // `NoteActions::trigger_note_action()` catches the typed exception and maps it |
| 121 | // to a 403 REST response. |
| 122 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 123 | throw new NoteActionForbiddenException( |
| 124 | esc_html__( 'You do not have permissions to manage plugins. Please contact your site administrator.', 'woocommerce' ) |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | $this->install_and_activate_plugin( 'jetpack' ); |
| 129 | $this->install_and_activate_plugin( 'woocommerce-services' ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Installs and activates the specified plugin. |
| 134 | * |
| 135 | * @param string $plugin The plugin slug. |
| 136 | */ |
| 137 | private function install_and_activate_plugin( $plugin ) { |
| 138 | $install_request = array( 'plugin' => $plugin ); |
| 139 | $installer = new \Automattic\WooCommerce\Admin\API\OnboardingPlugins(); |
| 140 | $result = $installer->install_plugin( $install_request ); |
| 141 | |
| 142 | // @todo Use the error statuses to decide whether or not to action the note. |
| 143 | if ( is_wp_error( $result ) ) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | $activate_request = array( 'plugins' => $plugin ); |
| 148 | |
| 149 | $installer->activate_plugins( $activate_request ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Create an alert notification in response to an error installing a plugin. |
| 154 | * |
| 155 | * @param string $slug The slug of the plugin being installed. |
| 156 | */ |
| 157 | public function on_install_error( $slug ) { |
| 158 | // Exit early if we're not installing the Jetpack or the WooCommerce Shipping & Tax plugins. |
| 159 | if ( 'jetpack' !== $slug && 'woocommerce-services' !== $slug ) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | self::possibly_add_note(); |
| 164 | } |
| 165 | } |
| 166 |