n_info = false, $plugin_file = '' ) { $changelog = ''; if ( isset( $subscription->product->changelog ) ) { // We need to replace h2's and h3's with h4's because the styling expects that. $changelog = str_replace( 'product->changelog ) ); $changelog = str_replace( ' ( $plugin_info ) ? YOAST_SEO_WP_REQUIRED : null, ]; return (object) [ 'new_version' => ( $subscription->product->version ?? '' ), 'name' => $subscription->product->name, 'slug' => $subscription->product->slug, 'plugin' => $plugin_file, 'url' => $subscription->product->store_url, 'last_update' => $subscription->product->last_updated, 'homepage' => $subscription->product->store_url, 'download_link' => $subscription->product->download, 'package' => $subscription->product->download, 'sections' => [ 'changelog' => $changelog, 'support' => $this->get_support_section(), ], 'icons' => [ '2x' => $this->get_icon( $subscription->product->slug ), ], 'update_supported' => true, 'banners' => $this->get_banners( $subscription->product->slug ), // If we have extracted Yoast Free's data before, use that. If not, resort to the defaults. 'tested' => YOAST_SEO_WP_TESTED, 'requires' => ( $yoast_free_data->requires ?? $defaults['requires'] ), 'requires_php' => YOAST_SEO_PHP_REQUIRED, ]; } /** * Returns the plugin's icon URL. * * @param string $slug The plugin slug. * * @return string The icon URL for this plugin. */ protected function get_icon( $slug ) { switch ( $slug ) { case self::LOCAL_SLUG: return 'https://yoa.st/local-seo-icon'; case self::NEWS_SLUG: return 'https://yoa.st/news-seo-icon'; case self::PREMIUM_SLUG: return 'https://yoa.st/yoast-seo-icon'; case self::VIDEO_SLUG: return 'https://yoa.st/video-seo-icon'; case self::WOOCOMMERCE_SLUG: return 'https://yoa.st/woo-seo-icon'; } } /** * Return an array of plugin banner URLs. * * @param string $slug The plugin slug. * * @return string[] */ protected function get_banners( $slug ) { switch ( $slug ) { case self::LOCAL_SLUG: return [ 'high' => 'https://yoa.st/yoast-seo-banner-local', 'low' => 'https://yoa.st/yoast-seo-banner-low-local', ]; case self::NEWS_SLUG: return [ 'high' => 'https://yoa.st/yoast-seo-banner-news', 'low' => 'https://yoa.st/yoast-seo-banner-low-news', ]; case self::PREMIUM_SLUG: return [ 'high' => 'https://yoa.st/yoast-seo-banner-premium', 'low' => 'https://yoa.st/yoast-seo-banner-low-premium', ]; case self::VIDEO_SLUG: return [ 'high' => 'https://yoa.st/yoast-seo-banner-video', 'low' => 'https://yoa.st/yoast-seo-banner-low-video', ]; case self::WOOCOMMERCE_SLUG: return [ 'high' => 'https://yoa.st/yoast-seo-banner-woo', 'low' => 'https://yoa.st/yoast-seo-banner-low-woo', ]; } } /** * Checks if the given plugin_file belongs to a Yoast addon. * * @param string $plugin_file Path to the plugin. * * @return bool True when plugin file is for a Yoast addon. */ protected function is_yoast_addon( $plugin_file ) { return $this->get_slug_by_plugin_file( $plugin_file ) !== ''; } /** * Retrieves the addon slug by given plugin file path. * * @param string $plugin_file The file path to the plugin. * * @return string The slug when found or empty string when not. */ protected function get_slug_by_plugin_file( $plugin_file ) { $addons = self::$addons; // Yoast SEO Free isn't an addon, but we needed it in Premium to fetch translations. if ( YoastSEO()->helpers->product->is_premium() ) { $addons['wp-seo.php'] = self::FREE_SLUG; } foreach ( $addons as $addon => $addon_slug ) { if ( strpos( $plugin_file, $addon ) !== false ) { return $addon_slug; } } return ''; } /** * Retrieves the installed Yoast addons. * * @return array The installed plugins. */ protected function get_installed_addons() { return array_filter( $this->get_plugins(), [ $this, 'is_yoast_addon' ], ARRAY_FILTER_USE_KEY ); } /** * Retrieves a list of active addons. * * @return array The active addons. */ protected function get_active_addons() { return array_filter( $this->get_installed_addons(), [ $this, 'is_plugin_active' ], ARRAY_FILTER_USE_KEY ); } /** * Retrieves the current sites from the API. * * @codeCoverageIgnore * * @return bool|stdClass Object when request is successful. False if not. */ protected function request_current_sites() { return $this->get_site_information_default(); } /** * Retrieves the transient value with the site information. * * @codeCoverageIgnore * * @return stdClass|false The transient value. */ protected function get_site_information_transient() { global $pagenow; // Force re-check on license & dashboard pages. $current_page = null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. if ( isset( $_GET['page'] ) && is_string( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information, We are only strictly comparing and thus no need to sanitize. $current_page = wp_unslash( $_GET['page'] ); } // Check whether the licenses are valid or whether we need to show notifications. $quick = ( $current_page === Plans_Page_Integration::PAGE || $current_page === General_Page_Integration::PAGE ); // Also do a fresh request on Plugins & Core Update pages. $quick = $quick || $pagenow === 'plugins.php'; $quick = $quick || $pagenow === 'update-core.php'; if ( $quick ) { return get_transient( self::SITE_INFORMATION_TRANSIENT_QUICK ); } return get_transient( self::SITE_INFORMATION_TRANSIENT ); } /** * Sets the site information transient. * * @codeCoverageIgnore * * @param stdClass $site_information The site information to save. * * @return void */ protected function set_site_information_transient( $site_information ) { set_transient( self::SITE_INFORMATION_TRANSIENT, $site_information, DAY_IN_SECONDS ); set_transient( self::SITE_INFORMATION_TRANSIENT_QUICK, $site_information, 60 ); } /** * Retrieves all installed WordPress plugins. * * @codeCoverageIgnore * * @return array The plugins. */ protected function get_plugins() { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } return get_plugins(); } /** * Checks if the given plugin file belongs to an active plugin. * * @codeCoverageIgnore * * @param string $plugin_file The file path to the plugin. * * @return bool True when plugin is active. */ protected function is_plugin_active( $plugin_file ) { return is_plugin_active( $plugin_file ); } /** * Returns an object with no subscriptions. * * @codeCoverageIgnore * * @return stdClass Site information. */ protected function get_site_information_default() { return (object) [ 'url' => WPSEO_Utils::get_home_url(), 'subscriptions' => [], ]; } /** * Maps the plugin API response. * * @param object $site_information Site information as received from the API. * * @return stdClass Mapped site information. */ protected function map_site_information( $site_information ) { return (object) [ 'url' => $site_information->url, 'subscriptions' => array_map( [ $this, 'map_subscription' ], $site_information->subscriptions ), ]; } /** * Maps a plugin subscription. * * @param object $subscription Subscription information as received from the API. * * @return stdClass Mapped subscription. */ protected function map_subscription( $subscription ) { // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Not our properties. return (object) [ 'renewal_url' => $subscription->renewalUrl, 'expiry_date' => $subscription->expiryDate, 'product' => (object) [ 'version' => $subscription->product->version, 'name' => $subscription->product->name, 'slug' => $subscription->product->slug, 'last_updated' => $subscription->product->lastUpdated, 'store_url' => $subscription->product->storeUrl, // Ternary operator is necessary because download can be undefined. 'download' => ( $subscription->product->download ?? null ), 'changelog' => $subscription->product->changelog, ], ]; // phpcs:enable } /** * Retrieves the site information. * * @return stdClass The site information. */ private function get_site_information() { if ( ! $this->has_installed_addons() ) { return $this->get_site_information_default(); } return $this->get_myyoast_site_information(); } /** * Retrieves the contents for the support section. * * @return string The support section content. */ protected function get_support_section() { return '

' . __( 'Need support?', 'wordpress-seo' ) . '

' . '

' /* translators: 1: expands to that refers to the help page, 2: closing tag. */ . sprintf( __( 'You can probably find an answer to your question in our %1$shelp center%2$s.', 'wordpress-seo' ), '', '' ) . ' ' /* translators: %s expands to a mailto support link. */ . sprintf( __( 'If you still need support and have an active subscription for this product, please email %s.', 'wordpress-seo' ), 'support@yoast.com' ) . '

'; } }