Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Adds filters to allow sideloading of the Jetpack Backup plugin through JITMs.
38 changes: 38 additions & 0 deletions projects/packages/jitm/src/class-post-connection-jitm.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,40 @@ public static function jitm_jetpack_creative_mail_activate() {
);
}

/**
* A special filter used in the CTA of a JITM offering to install the Jetpack Backup plugin.
*
* @return string The new CTA
*/
public static function jitm_jetpack_backup_install() {
return wp_nonce_url(
add_query_arg(
array(
'jetpack-backup-action' => 'install',
),
admin_url( 'admin.php?page=jetpack' )
),
'jetpack-backup-install'
);
}

/**
* A special filter used in the CTA of a JITM offering to activate the Jetpack Backup plugin.
*
* @return string The new CTA
*/
public static function jitm_jetpack_backup_activate() {
return wp_nonce_url(
add_query_arg(
array(
'jetpack-backup-action' => 'activate',
),
admin_url( 'admin.php?page=jetpack' )
),
'jetpack-backup-install'
);
}

/**
* This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
* the connection owner.
Expand Down Expand Up @@ -366,6 +400,10 @@ public function get_messages( $message_path, $query, $full_jp_logo_exists ) {
add_filter( 'jitm_jetpack_creative_mail_install', array( $this, 'jitm_jetpack_creative_mail_install' ) );
add_filter( 'jitm_jetpack_creative_mail_activate', array( $this, 'jitm_jetpack_creative_mail_activate' ) );

// Jetpack Backup.
add_filter( 'jitm_jetpack_backup_install', array( $this, 'jitm_jetpack_backup_install' ) );
add_filter( 'jitm_jetpack_backup_activate', array( $this, 'jitm_jetpack_backup_activate' ) );

$user = wp_get_current_user();

// Unauthenticated or invalid requests just bail.
Expand Down
1 change: 1 addition & 0 deletions projects/plugins/jetpack/3rd-party/3rd-party.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function load_3rd_party() {
'class-jetpack-crm-data.php',
'class-jetpack-modules-overrides.php', // Special case. Tools to be used to override module settings.
'creative-mail.php',
'jetpack-backup.php',
'debug-bar.php',
'class-domain-mapping.php',
'crowdsignal.php',
Expand Down
132 changes: 132 additions & 0 deletions projects/plugins/jetpack/3rd-party/jetpack-backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Compatibility functions for the Jetpack Backup plugin.
* https://wordpress.org/plugins/jetpack-backup/
*
* @since 10.4
*
* @package automattic/jetpack
*/

namespace Automattic\Jetpack\Jetpack_Backup;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

const PLUGIN_SLUG = 'jetpack-backup';
const PLUGIN_FILE = 'jetpack-backup/jetpack-backup.php';

add_action( 'admin_notices', __NAMESPACE__ . '\error_notice' );
add_action( 'admin_init', __NAMESPACE__ . '\try_install' );
add_action( 'jetpack_activated_plugin', __NAMESPACE__ . '\configure_plugin', 10, 2 );

/**
* Verify the intent to install Jetpack Backup, and kick off installation.
*
* This works in tandem with a JITM set up in the JITM package.
*/
function try_install() {
if ( ! isset( $_GET['jetpack-backup-action'] ) ) {
return;
}

check_admin_referer( 'jetpack-backup-install' );

$result = false;
// If the plugin install fails, redirect to plugin install page pre-populated with jetpack-backup search term.
$redirect_on_error = admin_url( 'plugin-install.php?s=jetpack-backup&tab=search&type=term' );

// Attempt to install and activate the plugin.
if ( current_user_can( 'activate_plugins' ) ) {
switch ( $_GET['jetpack-backup-action'] ) {
case 'install':
$result = install_and_activate();
break;
case 'activate':
$result = activate();
break;
}
}

$jetpack = \Jetpack::init();
if ( $result ) {
/** This action is already documented in _inc/lib/class.core-rest-api-endpoints.php */
do_action( 'jetpack_activated_plugin', PLUGIN_FILE, 'jitm' );
$jetpack->stat( 'jitm-plugin-install', 'jetpack-backup-success' );
Comment thread
dereksmart marked this conversation as resolved.
Resolved
$redirect = admin_url( 'admin.php?page=jetpack-backup' );
} else {
$jetpack->stat( 'jitm-plugin-install', 'jetpack-backup-fail' );
$redirect = add_query_arg( 'jetpack-backup-install-error', true, $redirect_on_error );
}

wp_safe_redirect( $redirect );

exit;
}

/**
* Install and activate the Jetpack Backup plugin.
*
* @return bool result of installation
*/
function install_and_activate() {
jetpack_require_lib( 'plugins' );
$result = \Jetpack_Plugins::install_and_activate_plugin( PLUGIN_SLUG );

if ( is_wp_error( $result ) ) {
return false;
} else {
return true;
}
}

/**
* Activate the Jetpack Backup plugin.
*
* @return bool result of activation
*/
function activate() {
$result = activate_plugin( PLUGIN_FILE );

// Activate_plugin() returns null on success.
return is_null( $result );
}

/**
* Notify the user that the installation of Jetpack Backup failed.
*/
function error_notice() {
if ( empty( $_GET['jetpack-backup-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}

?>
<div class="notice notice-error is-dismissible">
<p><?php esc_html_e( 'There was an error installing Jetpack Backup. Please try again.', 'jetpack' ); ?></p>
</div>
<?php
}

/**
* Set some options when first activating the plugin via Jetpack.
*
* @since 8.9.0
*
* @param string $plugin_file Plugin file.
* @param string $source Where did the plugin installation originate.
*/
function configure_plugin( $plugin_file, $source ) {
if ( PLUGIN_FILE !== $plugin_file ) {
return;
}

$plugin_info = array(
'plugin' => 'jetpack',
'version' => JETPACK__VERSION,
'time' => time(),
'source' => esc_attr( $source ),
);

update_option( 'jetpack_backup_referred_by', $plugin_info );
Comment thread
dereksmart marked this conversation as resolved.
Resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: compat

Jetpack Backup: Add considtions that allow sideloading of Jetpack Backup plugin via JITMs.