{
if ( empty( $response->package ) && isset( $response->unavailability_reason ) ) {
$message = $this->get_update_message( $response->unavailability_reason );
echo ' ' . wp_kses_post( $message ) . '';
}
}
}
/**
* Get unavailability reason message.
*
* @param string $reason Unavailability reason ID, like 'not_connected'.
* @param mixed $default Default text to return when specified ID has no message attached to it.
* @return string
*/
public function get_update_message( $reason = '', $default = null ) {
if ( is_null( $default ) ) {
$default = '';
}
$unavailability_reasons = [
'update_free' => __( 'Please update the free version before updating Rank Math SEO PRO.', 'rank-math-pro' ),
'not_subscribed' => sprintf(
/* translators: 1: Plugin name, 2: Pricing Link's opening HTML anchor tag, 3: Pricing Link's closing HTML anchor tag. */
__( 'It seems that you don\'t have an active subscription for %1$s. Please see %2$sdetails and pricing%3$s.', 'rank-math-pro' ),
'Rank Math SEO PRO',
'',
''
),
'not_connected' => sprintf(
/* translators: 1: Link's opening HTML anchor tag, 2: Link's closing HTML anchor tag. */
__( 'Please %1$s connect Rank Math SEO PRO %2$s for automatic updates.', 'rank-math-pro' ),
'',
''
),
];
$unavailability_reasons = $this->do_filter( 'updates/unavailability_reasons', $unavailability_reasons );
if ( isset( $unavailability_reasons[ $reason ] ) ) {
return $unavailability_reasons[ $reason ];
}
return $default;
}
/**
* Toggle auto updates option.
* Hooked to run when the option is changed in the free version.
*
* @param string $toggle New status.
* @return void
*/
public function toggle_auto_update( $toggle ) {
$auto_updates = (array) get_site_option( 'auto_update_plugins', [] );
if ( ! empty( $toggle ) && 'off' !== $toggle ) {
$auto_updates[] = 'seo-by-rank-math-pro/rank-math-pro.php';
update_site_option( 'auto_update_plugins', array_unique( $auto_updates ) );
return;
}
update_site_option( 'auto_update_plugins', array_diff( $auto_updates, [ 'seo-by-rank-math-pro/rank-math-pro.php' ] ) );
}
/**
* Don't auto-update if it's a beta version.
*
* @param bool $update Whether to update the plugin or not.
* @param array $item The update plugin object.
*
* @return bool
*/
public function auto_update_plugin( $update, $item ) {
// Show auto-updates control on Plugins page.
if ( did_action( 'load-plugins.php' ) ) {
return $update;
}
if ( $this->is_rm_pro_update( $item ) && $this->is_beta_update( $item ) ) {
return false;
}
return $update;
}
/**
* Check if updatable object is RM.
*
* @param object $item Updatable object.
* @return boolean
*/
public function is_rm_pro_update( $item ) {
return isset( $item->slug ) &&
'seo-by-rank-math-pro' === $item->slug &&
isset( $item->new_version );
}
/**
* Check if given version is beta.
*
* @param object $item Update data.
* @return boolean
*/
public function is_beta_update( $item ) {
return ( is_object( $item ) && isset( $item->new_version ) && false !== stripos( $item->new_version, 'beta' ) );
}
/**
* Hide "update scheduled in X hours" message if update is a beta version because we don't auto-update those.
*
* @param string $html HTML string.
* @param string $plugin_file Plugin file relative to the plugin directory.
* @param array $plugin_data Plugin update data.
* @return string
*/
public function plugin_auto_update_setting_html( $html, $plugin_file, $plugin_data ) {
if ( 'seo-by-rank-math-pro/rank-math-pro.php' !== $plugin_file ) {
return $html;
}
if ( ! empty( $plugin_data['is_beta'] ) ) {
$html = str_replace( 'class="auto-update-time"', 'class="auto-update-time hidden"', $html );
}
return $html;
}
/**
* Set should_send to true if Pro update is available, to send the notification email.
* Notification feature is only available if auto-updates are disabled.
*
* @param boolean $should_send Original should_send value.
* @param mixed $transient update_plugins site ransient value.
* @return boolean
*/
public function should_send_update_notification( $should_send, $transient ) {
if ( $this->get_auto_update_setting() ) {
return $should_send;
}
if ( ! $this->is_update_available( $transient ) ) {
return $should_send;
}
$new_version = $transient->response['seo-by-rank-math-pro/rank-math-pro.php']->new_version;
// Now let's check if we've already sent this email.
$sent = get_option( 'rank_math_update_notifications_sent', [ 'pro' => [ 'new_version' => '1.0' ] ] );
if ( ! isset( $sent['pro'] ) ) {
$sent['pro'] = [ 'new_version' => '1.0' ];
}
if ( version_compare( $sent['pro']['new_version'], $new_version, '>=' ) ) {
return $should_send;
}
return true;
}
/**
* Add Pro to notification product list if an update is available.
*
* @param array $products Products data array.
* @param mixed $transient Transient value.
* @return array
*/
public function update_notification_products( $products, $transient ) {
if ( $this->get_auto_update_setting() ) {
return $products;
}
if ( ! $this->is_update_available( $transient ) ) {
return $products;
}
$products['pro'] = [
'name' => __( 'Rank Math PRO', 'rank-math-pro' ),
'old_version' => RANK_MATH_PRO_VERSION,
'new_version' => $transient->response['seo-by-rank-math-pro/rank-math-pro.php']->new_version,
'changelog' => KB::get( 'changelog', 'PRO Update Popup Chagelog Link' ),
];
return $products;
}
/**
* Check if update is available for the PRO version.
*
* @param mixed $transient The update_plugins transient value.
* @return boolean
*/
public function is_update_available( $transient ) {
if ( ! is_object( $transient )
|| empty( $transient->response )
|| empty( $transient->response['seo-by-rank-math-pro/rank-math-pro.php'] )
|| empty( $transient->response['seo-by-rank-math-pro/rank-math-pro.php']->new_version )
) {
return false;
}
return true;
}
/**
* Get Auto update setting status.
*
* @return bool
*/
public function get_auto_update_setting() {
return in_array( 'seo-by-rank-math-pro/rank-math-pro.php', (array) get_site_option( 'auto_update_plugins', [] ), true );
}
/**
* Connect auto-update toggles: if we enable it for the Pro, then Free should be enabled too,
* and if we disable it for Free then it should be disabled for the Pro too.
*
* @param string $option Option name.
* @param mixed $value Option value.
* @param mixed $old_value Previous option value before the change.
* @param int $network_id Network ID.
* @return void
*/
public function connect_auto_update_toggles( $option, $value, $old_value, $network_id ) {
$this->remove_action( 'update_site_option_auto_update_plugins', 'connect_auto_update_toggles', 20 );
if ( ! is_array( $value ) ) {
return;
}
$free_file = 'seo-by-rank-math/rank-math.php';
$pro_file = 'seo-by-rank-math-pro/rank-math-pro.php';
// If we just enabled it for Rank Math SEO Pro.
if ( in_array( $pro_file, $value, true ) && ! in_array( $pro_file, $old_value, true ) && ! in_array( $free_file, $value, true ) ) {
$value[] = $free_file;
update_site_option( $option, $value );
return;
}
// If we just disabled it for Rank Math SEO Free.
if ( ! in_array( $free_file, $value, true ) && in_array( $free_file, $old_value, true ) && in_array( $pro_file, $value, true ) ) {
$value = array_diff( $value, [ $pro_file ] );
update_site_option( $option, $value );
return;
}
}
}