'update_225', ); /** * Constructor. */ public function __construct() { add_action( 'init', array( $this, 'check_update' ), 1 ); } /** * Check the plugin version and run the updater if necessary on all requests. * * @since 2.2.5 * @return void */ public function check_update(): void { $db_version = get_option( 'aicw_version' ); $current_version = ai_content_writer()->get_version(); $requires_update = version_compare( $db_version, $current_version, '<' ); $can_install = ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && ! defined( 'IFRAME_REQUEST' ); if ( $can_install && $requires_update ) { static::install(); $update_versions = array_keys( $this->updates ); usort( $update_versions, 'version_compare' ); if ( ! is_null( $db_version ) && version_compare( $db_version, end( $update_versions ), '<' ) ) { $this->update(); } else { update_option( 'aicw_version', $current_version ); } } } /** * Update the plugin to the new version. * * @since 2.2.5 * @return void */ public function update(): void { $db_version = get_option( 'aicw_version' ); foreach ( $this->updates as $version => $callbacks ) { $callbacks = (array) $callbacks; if ( version_compare( $db_version, $version, '<' ) ) { foreach ( $callbacks as $callback ) { call_user_func( array( $this, $callback ) ); } update_option( 'aicw_version', $version ); $notice = sprintf( /* translators: 1: plugin name 2: version number */ __( '%1$s updated to version %2$s successfully.', 'ai-content-writer' ), __( 'AI Content Writer', 'ai-content-writer' ), $version ); ai_content_writer()->flash_notice( $notice ); } } } /** * Install the plugin. * * @since 2.2.5 * @return void */ public static function install(): void { if ( ! is_blog_installed() ) { return; } add_option( 'aicw_version', ai_content_writer()->get_version() ); add_option( 'aicw_installed', wp_date( 'U' ) ); flush_rewrite_rules(); } /** * Upgrade to 2.2.5 * * @since 2.2.5 * @return void */ public function update_225(): void { $old_hooks = array( 'aicw_generate_title', 'aicw_generate_content', 'aicw_generate_thumbnail', 'aicw_publish_posts', ); foreach ( $old_hooks as $hook ) { if ( wp_next_scheduled( $hook ) ) { wp_clear_scheduled_hook( $hook ); } } $campaigns = get_posts( array( 'post_type' => 'aicw_campaign', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => array( array( 'key' => '_aicw_campaign_frequency', 'compare' => 'NOT EXISTS', ), ), ) ); $old_frequency = 'hourly'; $has_frequency = get_option( 'aicwp_campaign_frequency' ); if ( $has_frequency ) { $old_frequency = $has_frequency; delete_option( 'aicwp_campaign_frequency' ); } foreach ( $campaigns as $campaign_id ) { update_post_meta( $campaign_id, '_aicw_campaign_frequency', $old_frequency ); Cron::schedule_campaign( $campaign_id, $old_frequency ); } } }