Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Admin/Notes
File: InstallJPAndWCSPlugins.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WooCommerce Admin Add Install Jetpack and WooCommerce Shipping & Tax Plugin Note Provider.
[2] Fix | Delete
*
[3] Fix | Delete
* Adds a note to the merchant's inbox prompting them to install the Jetpack
[4] Fix | Delete
* and WooCommerce Shipping & Tax plugins after it fails to install during
[5] Fix | Delete
* WooCommerce setup.
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
namespace Automattic\WooCommerce\Internal\Admin\Notes;
[9] Fix | Delete
[10] Fix | Delete
defined( 'ABSPATH' ) || exit;
[11] Fix | Delete
[12] Fix | Delete
use Automattic\WooCommerce\Admin\Notes\Note;
[13] Fix | Delete
use Automattic\WooCommerce\Admin\Notes\Notes;
[14] Fix | Delete
use Automattic\WooCommerce\Admin\Notes\NoteTraits;
[15] Fix | Delete
use Automattic\WooCommerce\Admin\PluginsHelper;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Install_JP_And_WCS_Plugins
[19] Fix | Delete
*/
[20] Fix | Delete
class InstallJPAndWCSPlugins {
[21] Fix | Delete
/**
[22] Fix | Delete
* Note traits.
[23] Fix | Delete
*/
[24] Fix | Delete
use NoteTraits;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Name of the note for use in the database.
[28] Fix | Delete
*/
[29] Fix | Delete
const NOTE_NAME = 'wc-admin-install-jp-and-wcs-plugins';
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Constructor.
[33] Fix | Delete
*/
[34] Fix | Delete
public function __construct() {
[35] Fix | Delete
add_action( 'woocommerce_note_action_install-jp-and-wcs-plugins', array( $this, 'install_jp_and_wcs_plugins' ) );
[36] Fix | Delete
add_action( 'activated_plugin', array( $this, 'action_note' ) );
[37] Fix | Delete
add_action( 'woocommerce_plugins_install_api_error', array( $this, 'on_install_error' ) );
[38] Fix | Delete
add_action( 'woocommerce_plugins_install_error', array( $this, 'on_install_error' ) );
[39] Fix | Delete
add_action( 'woocommerce_plugins_activate_error', array( $this, 'on_install_error' ) );
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Get the note.
[44] Fix | Delete
*
[45] Fix | Delete
* @return Note
[46] Fix | Delete
*/
[47] Fix | Delete
public static function get_note() {
[48] Fix | Delete
$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' );
[49] Fix | Delete
[50] Fix | Delete
$note = new Note();
[51] Fix | Delete
$note->set_title( __( 'Uh oh... There was a problem during the Jetpack and WooCommerce Shipping & Tax install. Please try again.', 'woocommerce' ) );
[52] Fix | Delete
$note->set_content( $content );
[53] Fix | Delete
$note->set_content_data( (object) array() );
[54] Fix | Delete
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
[55] Fix | Delete
$note->set_name( self::NOTE_NAME );
[56] Fix | Delete
$note->set_source( 'woocommerce-admin' );
[57] Fix | Delete
$note->add_action(
[58] Fix | Delete
'install-jp-and-wcs-plugins',
[59] Fix | Delete
__( 'Install plugins', 'woocommerce' ),
[60] Fix | Delete
false,
[61] Fix | Delete
Note::E_WC_ADMIN_NOTE_ACTIONED
[62] Fix | Delete
);
[63] Fix | Delete
return $note;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Action the Install Jetpack and WooCommerce Shipping & Tax note, if any exists,
[68] Fix | Delete
* and as long as both the Jetpack and WooCommerce Shipping & Tax plugins have been
[69] Fix | Delete
* activated.
[70] Fix | Delete
*/
[71] Fix | Delete
public static function action_note() {
[72] Fix | Delete
// Make sure that both plugins are active before actioning the note.
[73] Fix | Delete
$active_plugin_slugs = PluginsHelper::get_active_plugin_slugs();
[74] Fix | Delete
$jp_active = in_array( 'jetpack', $active_plugin_slugs, true );
[75] Fix | Delete
$wcs_active = in_array( 'woocommerce-services', $active_plugin_slugs, true );
[76] Fix | Delete
[77] Fix | Delete
if ( ! $jp_active || ! $wcs_active ) {
[78] Fix | Delete
return;
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
// Action any notes with a matching name.
[82] Fix | Delete
$data_store = Notes::load_data_store();
[83] Fix | Delete
$note_ids = $data_store->get_notes_with_name( self::NOTE_NAME );
[84] Fix | Delete
[85] Fix | Delete
foreach ( $note_ids as $note_id ) {
[86] Fix | Delete
$note = Notes::get_note( $note_id );
[87] Fix | Delete
[88] Fix | Delete
if ( $note ) {
[89] Fix | Delete
$note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED );
[90] Fix | Delete
$note->save();
[91] Fix | Delete
}
[92] Fix | Delete
}
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Install the Jetpack and WooCommerce Shipping & Tax plugins in response to the action
[97] Fix | Delete
* being clicked in the admin note.
[98] Fix | Delete
*
[99] Fix | Delete
* @param Note $note The note being actioned.
[100] Fix | Delete
*/
[101] Fix | Delete
public function install_jp_and_wcs_plugins( $note ) {
[102] Fix | Delete
if ( self::NOTE_NAME !== $note->get_name() ) {
[103] Fix | Delete
return;
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
$this->install_and_activate_plugin( 'jetpack' );
[107] Fix | Delete
$this->install_and_activate_plugin( 'woocommerce-services' );
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Installs and activates the specified plugin.
[112] Fix | Delete
*
[113] Fix | Delete
* @param string $plugin The plugin slug.
[114] Fix | Delete
*/
[115] Fix | Delete
private function install_and_activate_plugin( $plugin ) {
[116] Fix | Delete
$install_request = array( 'plugin' => $plugin );
[117] Fix | Delete
$installer = new \Automattic\WooCommerce\Admin\API\OnboardingPlugins();
[118] Fix | Delete
$result = $installer->install_plugin( $install_request );
[119] Fix | Delete
[120] Fix | Delete
// @todo Use the error statuses to decide whether or not to action the note.
[121] Fix | Delete
if ( is_wp_error( $result ) ) {
[122] Fix | Delete
return;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
$activate_request = array( 'plugins' => $plugin );
[126] Fix | Delete
[127] Fix | Delete
$installer->activate_plugins( $activate_request );
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
/**
[131] Fix | Delete
* Create an alert notification in response to an error installing a plugin.
[132] Fix | Delete
*
[133] Fix | Delete
* @param string $slug The slug of the plugin being installed.
[134] Fix | Delete
*/
[135] Fix | Delete
public function on_install_error( $slug ) {
[136] Fix | Delete
// Exit early if we're not installing the Jetpack or the WooCommerce Shipping & Tax plugins.
[137] Fix | Delete
if ( 'jetpack' !== $slug && 'woocommerce-services' !== $slug ) {
[138] Fix | Delete
return;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
self::possibly_add_note();
[142] Fix | Delete
}
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function