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' => __( 'https://rankmath.com/changelog/', 'rank-math-pro' ), ]; 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; } } }