settings = $settings; add_action('admin_init', array($this, 'maybe_set_install_date')); add_action('aifi_after_generate', array($this, 'increment_count'), 10, 2); add_action('aifi_after_generate_inline', array($this, 'increment_count'), 10, 2); add_action('admin_notices', array($this, 'render_notice')); add_action('rest_api_init', array($this, 'register_route')); } /** * Stamp the install date the first time we see this admin user. For users * upgrading from an older version, this becomes the upgrade date, the * 7-day clock simply starts then. */ public function maybe_set_install_date() { if (get_option(self::INSTALL_DATE_OPTION) === false) { update_option(self::INSTALL_DATE_OPTION, time()); } } /** * Bumps the successful generation counter. Hooked to the existing * aifi_after_generate / aifi_after_generate_inline actions in Generator. * * @param int $attachment_id * @param int $post_id */ public function increment_count($attachment_id, $post_id) { unset($attachment_id, $post_id); $count = (int) get_option(self::COUNT_OPTION, 0); update_option(self::COUNT_OPTION, $count + 1, false); } /** * Decide whether the notice should appear on the current screen. * * @return bool */ private function should_display() { if (!current_user_can( Settings::capability() )) { return false; } $state = get_option(self::STATE_OPTION, ''); if ($state === 'done' || $state === 'never') { return false; } if (strpos((string) $state, 'dismissed:') === 0) { $ts = (int) substr($state, strlen('dismissed:')); if ($ts > 0 && (time() - $ts) < self::RESNOOZE_DAYS * DAY_IN_SECONDS) { return false; } } $install_date = (int) get_option(self::INSTALL_DATE_OPTION, 0); if ($install_date <= 0) { return false; } if ((time() - $install_date) < self::MIN_DAYS_INSTALLED * DAY_IN_SECONDS) { return false; } $count = (int) get_option(self::COUNT_OPTION, 0); if ($count < self::MIN_GENERATIONS) { return false; } return $this->is_aifi_screen(); } /** * True if we're on an AIFIG-relevant admin screen, the settings page or * a post-edit screen for an enabled post type. * * @return bool */ private function is_aifi_screen() { if (!function_exists('get_current_screen')) { return false; } $screen = get_current_screen(); if (!$screen) { return false; } if ($screen->id === 'toplevel_page_ai-featured-image-generator') { return true; } if (in_array($screen->base, array('post'), true)) { $enabled = $this->settings->get_enabled_post_types(); if (!empty($screen->post_type) && in_array($screen->post_type, $enabled, true)) { return true; } } return false; } /** * Render the review request notice. */ public function render_notice() { if (!$this->should_display()) { return; } $nonce = wp_create_nonce('wp_rest'); $rest = esc_url_raw(rest_url('aifi/v1/review-state')); ?>
'POST', 'callback' => array($this, 'handle_state_change'), 'permission_callback' => function() { return current_user_can( Settings::capability() ); }, 'args' => array( 'state' => array( 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), ), )); } /** * Handle a state change request from the notice's JS. * * @param \WP_REST_Request $request * @return \WP_REST_Response|\WP_Error */ public function handle_state_change($request) { $state = (string) $request->get_param('state'); switch ($state) { case 'dismissed': update_option(self::STATE_OPTION, 'dismissed:' . time(), false); break; case 'done': update_option(self::STATE_OPTION, 'done', false); break; case 'never': update_option(self::STATE_OPTION, 'never', false); break; default: return new \WP_Error('aifi_invalid_state', __('Invalid state.', 'ai-featured-image-generator'), array('status' => 400)); } return rest_ensure_response(array('ok' => true)); } }