ublic function isSpecialPage( $postId = false ) { if ( (int) get_option( 'page_for_posts' ) === (int) $postId || (int) get_option( 'wp_page_for_privacy_policy' ) === (int) $postId || $this->isBuddyPressPage( $postId ) || $this->isWooCommercePage( $postId ) ) { return true; } return false; } /** * Returns the page number of the current page. * * @since 4.0.0 * * @return int The page number. */ public function getPageNumber() { $page = get_query_var( 'page' ); if ( ! empty( $page ) ) { return (int) $page; } $paged = get_query_var( 'paged' ); if ( ! empty( $paged ) ) { return (int) $paged; } return 1; } /** * Returns the page number for the comment page. * * @since 4.2.1 * * @return int|false The page number or false if we're not on a comment page. */ public function getCommentPageNumber() { $cpage = get_query_var( 'cpage' ); return ! empty( $cpage ) ? (int) $cpage : false; } /** * Check if the post passed in is a valid post, not a revision or autosave. * * @since 4.0.5 * * @param WP_Post $post The Post object to check. * @param array $allowedPostStatuses Allowed post statuses. * @return bool True if valid, false if not. */ public function isValidPost( $post, $allowedPostStatuses = [ 'publish' ] ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return false; } if ( ! is_object( $post ) ) { $post = get_post( $post ); } // No post, no go. if ( empty( $post ) ) { return false; } // In order to prevent recursion, we are skipping scheduled-action posts and revisions. if ( 'scheduled-action' === $post->post_type || 'revision' === $post->post_type ) { return false; } // Ensure this post has the proper post status. if ( ! in_array( $post->post_status, $allowedPostStatuses, true ) && ! in_array( 'all', $allowedPostStatuses, true ) ) { return false; } return true; } /** * Checks whether the given URL is a valid attachment. * * @since 4.0.13 * * @param string $url The URL. * @return boolean Whether the URL is a valid attachment. */ public function isValidAttachment( $url ) { $uploadDirUrl = aioseo()->helpers->escapeRegex( $this->getWpContentUrl() ); return preg_match( "/$uploadDirUrl.*/", $url ); } /** * Tries to convert an attachment URL into a post ID. * * This our own optimized version of attachment_url_to_postid(). * * @since 4.0.13 * * @param string $url The attachment URL. * @return int|boolean The attachment ID or false if no attachment could be found. */ public function attachmentUrlToPostId( $url ) { $cacheName = 'attachment_url_to_post_id_' . sha1( "aioseo_attachment_url_to_post_id_$url" ); $cachedId = aioseo()->core->cache->get( $cacheName ); if ( $cachedId ) { return 'none' !== $cachedId && is_numeric( $cachedId ) ? (int) $cachedId : false; } $path = $url; $uploadDirInfo = wp_get_upload_dir(); $siteUrl = wp_parse_url( $uploadDirInfo['url'] ); $imagePath = wp_parse_url( $path ); // Force the protocols to match if needed. if ( isset( $imagePath['scheme'] ) && ( $imagePath['scheme'] !== $siteUrl['scheme'] ) ) { $path = str_replace( $imagePath['scheme'], $siteUrl['scheme'], $path ); } if ( ! $this->isValidAttachment( $path ) ) { aioseo()->core->cache->update( $cacheName, 'none' ); return false; } if ( 0 === strpos( $path, $uploadDirInfo['baseurl'] . '/' ) ) { $path = substr( $path, strlen( $uploadDirInfo['baseurl'] . '/' ) ); } $results = aioseo()->core->db->start( 'postmeta' ) ->select( 'post_id' ) ->where( 'meta_key', '_wp_attached_file' ) ->where( 'meta_value', $path ) ->limit( 1 ) ->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(); } /** * Checks whether we're on the given screen. * * @since 4.0.7 * * @param string $screenName The screen name. * @return boolean Whether we're on the given screen. */ public function isScreenBase( $screenName ) { $screen = $this->getCurrentScreen(); if ( ! $screen || ! isset( $screen->base ) ) { return false; } 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 */ public function isScreenPostType( $postType ) { $screen = $this->getCurrentScreen(); if ( ! $screen || ! isset( $screen->post_type ) ) { return false; } return $screen->post_type === $postType; } /** * 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() { $self = ! empty( $_SERVER['PHP_SELF'] ) ? wp_unslash( $_SERVER['PHP_SELF'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if ( preg_match( '/wp-login\.php$|wp-register\.php$/', $self ) ) { return true; } return false; } }