->run() ->result(); if ( empty( $results[0]->post_id ) ) { aioseo()->core->cache->update( $cacheName, 'none' ); return false; } aioseo()->core->cache->update( $cacheName, $results[0]->post_id ); return $results[0]->post_id; } /** * Returns true if the request is a non-legacy REST API request. * This function was copied from WooCommerce and improved. * * @since 4.1.2 * * @return bool True if this is a REST API request. */ public function isRestApiRequest() { if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return true; } global $wp_rewrite; if ( empty( $wp_rewrite ) ) { return false; } if ( empty( $_SERVER['REQUEST_URI'] ) ) { return false; } $restUrl = wp_parse_url( get_rest_url() ); $restUrl = $restUrl['path'] . ( ! empty( $restUrl['query'] ) ? '?' . $restUrl['query'] : '' ); $isRestApiRequest = ( 0 === strpos( $_SERVER['REQUEST_URI'], $restUrl ) ); return apply_filters( 'aioseo_is_rest_api_request', $isRestApiRequest ); } /** * Checks whether the current request is an AJAX, CRON or REST request. * * @since 4.1.3 * * @return bool Wether the request is an AJAX, CRON or REST request. */ public function isAjaxCronRestRequest() { return wp_doing_ajax() || wp_doing_cron() || $this->isRestApiRequest(); } /** * Check if we are in the middle of a WP-CLI call. * * @since 4.2.8 * * @return bool True if we are in the WP_CLI context. */ public function isDoingWpCli() { return defined( 'WP_CLI' ) && WP_CLI; } /** * Checks whether we're on the given screen. * * @since 4.0.7 * @version 4.3.1 * * @param string $screenName The screen name. * @param string $comparison Check as a prefix. * @return bool Whether we're on the given screen. */ public function isScreenBase( $screenName, $comparison = '' ) { $screen = $this->getCurrentScreen(); if ( ! $screen || ! isset( $screen->base ) ) { return false; } if ( 'prefix' === $comparison ) { return 0 === stripos( $screen->base, $screenName ); } return $screen->base === $screenName; } /** * Returns if current screen is of a post type * * @since 4.0.17 * * @param string $postType Post type slug * @return bool True if the current screen is a post type screen. */ public function isScreenPostType( $postType ) { $screen = $this->getCurrentScreen(); if ( ! $screen || ! isset( $screen->post_type ) ) { return false; } return $screen->post_type === $postType; } /** * Returns if current screen is a post list, optionaly of a post type. * * @since 4.2.4 * * @param string $postType Post type slug. * @return bool Is a post list. */ public function isScreenPostList( $postType = '' ) { $screen = $this->getCurrentScreen(); if ( ! $this->isScreenBase( 'edit' ) || empty( $screen->post_type ) ) { return false; } if ( ! empty( $postType ) && $screen->post_type !== $postType ) { return false; } return true; } /** * Returns if current screen is a post edit screen, optionaly of a post type. * * @since 4.2.4 * * @param string $postType Post type slug. * @return bool Is a post editing screen. */ public function isScreenPostEdit( $postType = '' ) { $screen = $this->getCurrentScreen(); if ( ! $this->isScreenBase( 'post' ) || empty( $screen->post_type ) ) { return false; } if ( ! empty( $postType ) && $screen->post_type !== $postType ) { return false; } return true; } /** * Gets current admin screen. * * @since 4.0.17 * * @return false|\WP_Screen|null */ public function getCurrentScreen() { if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) { return false; } return get_current_screen(); } /** * Checks whether the current site is a multisite subdomain. * * @since 4.1.9 * * @return bool Whether the current site is a subdomain. */ public function isSubdomain() { if ( ! is_multisite() ) { return false; } return apply_filters( 'aioseo_multisite_subdomain', is_subdomain_install() ); } /** * Returns if the current page is the login or register page. * * @since 4.2.1 * * @return bool Login or register page. */ public function isWpLoginPage() { // We can't sanitize the filename using sanitize_file_name() here because it will cause issues with custom login pages and certain plugins/themes where this function is not defined. $self = ! empty( $_SERVER['PHP_SELF'] ) ? wp_unslash( $_SERVER['PHP_SELF'] ) : ''; // phpcs:ignore HM.Security.ValidatedSanitizedInput.InputNotSanitized if ( preg_match( '/wp-login\.php$|wp-register\.php$/', $self ) ) { return true; } return false; } /** * Returns which type of WordPress page we're seeing. * It will only work if {@see \WP_Query::$queried_object} has been set. * * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#filter-hierarchy * * @since 4.2.8 * * @return string|null The template type or `null` if no match. */ public function getTemplateType() { static $type = null; if ( ! empty( $type ) ) { return $type; } if ( is_attachment() ) { $type = 'attachment'; } elseif ( is_single() ) { $type = 'single'; } elseif ( is_page() || $this->isStaticPostsPage() || $this->isWooCommerceShopPage() ) { $type = 'page'; } elseif ( is_author() ) { // An author page is an archive page, so it needs to be checked before `is_archive()`. $type = 'author'; } elseif ( is_tax() || is_category() || is_tag() ) { // A taxonomy term page is an archive page, so it needs to be checked before `is_archive()`. $type = 'taxonomy'; } elseif ( is_date() ) { // A date page is an archive page, so it needs to be checked before `is_archive()`. $type = 'date'; } elseif ( is_archive() ) { $type = 'archive'; } elseif ( is_home() && is_front_page() ) { $type = 'dynamic_home'; } elseif ( is_search() ) { $type = 'search'; } return $type; } /** * Sets the given post as the queried object of the main query. * * @since 4.3.0 * * @param \WP_Post|int $wpPost The post object or ID. * @return void */ public function setWpQueryPost( $wpPost ) { $wpPost = is_a( $wpPost, 'WP_Post' ) ? $wpPost : get_post( $wpPost ); global $wp_query, $post; $this->originalQuery = $this->deepClone( $wp_query ); $this->originalPost = is_a( $post, 'WP_Post' ) ? $this->deepClone( $post ) : null; $wp_query->posts = [ $wpPost ]; $wp_query->post = $wpPost; $wp_query->post_count = 1; $wp_query->get_queried_object_id = (int) $wpPost->ID; $wp_query->queried_object = $wpPost; $wp_query->is_single = true; $wp_query->is_singular = true; if ( 'page' === $wpPost->post_type ) { $wp_query->is_page = true; } $post = $wpPost; } /** * Restores the main query back to the original query. * * @since 4.3.0 * * @return void */ public function restoreWpQuery() { global $wp_query, $post; if ( is_a( $this->originalQuery, 'WP_Query' ) ) { // Loop over all properties and replace the ones that have changed. // We want to avoid replacing the entire object because it can cause issues with other plugins. foreach ( $this->originalQuery as $key => $value ) { if ( $value !== $wp_query->{$key} ) { $wp_query->{$key} = $value; } } } if ( is_a( $this->originalPost, 'WP_Post' ) ) { foreach ( $this->originalPost as $key => $value ) { if ( $value !== $post->{$key} ) { $post->{$key} = $value; } } } $this->originalQuery = null; $this->originalPost = null; } /** * Gets the list of theme features. * * @since 4.4.9 * * @return array List of theme features. */ public function getThemeFeatures() { global $_wp_theme_features; return isset( $_wp_theme_features ) && is_array( $_wp_theme_features ) ? $_wp_theme_features : []; } /** * Returns whether the active theme is a block-based theme or not. * * @since 4.5.3 * * @return bool Whether the active theme is a block-based theme or not. */ public function isBlockTheme() { if ( function_exists( 'wp_is_block_theme' ) ) { return wp_is_block_theme(); // phpcs:ignore AIOSEO.WpFunctionUse.NewFunctions.wp_is_block_themeFound } return false; } /** * Retrieves the website name. * * @since 4.6.1 * * @return string The website name. */ public function getWebsiteName() { return aioseo()->options->searchAppearance->global->schema->websiteName ? aioseo()->options->searchAppearance->global->schema->websiteName : aioseo()->helpers->decodeHtmlEntities( get_bloginfo( 'name' ) ); } }