* - `EP_NONE` * - `EP_ALL_ARCHIVES` * - `EP_ATTACHMENT` * - `EP_AUTHORS` * - `EP_CATEGORIES` * - `EP_COMMENTS` * - `EP_DATE` * - `EP_DAY` * - `EP_MONTH` * - `EP_PAGES` * - `EP_PERMALINK` * - `EP_ROOT` * - `EP_SEARCH` * - `EP_TAGS` * - `EP_YEAR` * @param string|bool $query_var Optional. Name of the corresponding query variable. Pass `false` to * skip registering a query_var for this endpoint. Defaults to the * value of `$name`. */ public function add_endpoint( $name, $places, $query_var = true ) { global $wp; // For backward compatibility, if null has explicitly been passed as `$query_var`, assume `true`. if ( true === $query_var || null === $query_var ) { $query_var = $name; } $this->endpoints[] = array( $places, $name, $query_var ); if ( $query_var ) { $wp->add_query_var( $query_var ); } } /** * Adds a new permalink structure. * * A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; * it is an easy way of expressing a set of regular expressions that rewrite to a set of * query strings. The new permastruct is added to the WP_Rewrite::$extra_permastructs array. * * When the rewrite rules are built by WP_Rewrite::rewrite_rules(), all of these extra * permastructs are passed to WP_Rewrite::generate_rewrite_rules() which transforms them * into the regular expressions that many love to hate. * * The `$args` parameter gives you control over how WP_Rewrite::generate_rewrite_rules() * works on the new permastruct. * * @since 2.5.0 * * @param string $name Name for permalink structure. * @param string $struct Permalink structure (e.g. category/%category%) * @param array $args { * Optional. Arguments for building rewrite rules based on the permalink structure. * Default empty array. * * @type bool $with_front Whether the structure should be prepended with `WP_Rewrite::$front`. * Default true. * @type int $ep_mask The endpoint mask defining which endpoints are added to the structure. * Accepts a mask of: * - `EP_ALL` * - `EP_NONE` * - `EP_ALL_ARCHIVES` * - `EP_ATTACHMENT` * - `EP_AUTHORS` * - `EP_CATEGORIES` * - `EP_COMMENTS` * - `EP_DATE` * - `EP_DAY` * - `EP_MONTH` * - `EP_PAGES` * - `EP_PERMALINK` * - `EP_ROOT` * - `EP_SEARCH` * - `EP_TAGS` * - `EP_YEAR` * Default `EP_NONE`. * @type bool $paged Whether archive pagination rules should be added for the structure. * Default true. * @type bool $feed Whether feed rewrite rules should be added for the structure. Default true. * @type bool $forcomments Whether the feed rules should be a query for a comments feed. Default false. * @type bool $walk_dirs Whether the 'directories' making up the structure should be walked over * and rewrite rules built for each in-turn. Default true. * @type bool $endpoints Whether endpoints should be applied to the generated rules. Default true. * } */ public function add_permastruct( $name, $struct, $args = array() ) { // Back-compat for the old parameters: $with_front and $ep_mask. if ( ! is_array( $args ) ) { $args = array( 'with_front' => $args ); } if ( func_num_args() === 4 ) { $args['ep_mask'] = func_get_arg( 3 ); } $defaults = array( 'with_front' => true, 'ep_mask' => EP_NONE, 'paged' => true, 'feed' => true, 'forcomments' => false, 'walk_dirs' => true, 'endpoints' => true, ); $args = array_intersect_key( $args, $defaults ); $args = wp_parse_args( $args, $defaults ); if ( $args['with_front'] ) { $struct = $this->front . $struct; } else { $struct = $this->root . $struct; } $args['struct'] = $struct; $this->extra_permastructs[ $name ] = $args; } /** * Removes a permalink structure. * * @since 4.5.0 * * @param string $name Name for permalink structure. */ public function remove_permastruct( $name ) { unset( $this->extra_permastructs[ $name ] ); } /** * Removes rewrite rules and then recreate rewrite rules. * * Calls WP_Rewrite::wp_rewrite_rules() after removing the 'rewrite_rules' option. * If the function named 'save_mod_rewrite_rules' exists, it will be called. * * @since 2.0.1 * * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard). */ public function flush_rules( $hard = true ) { static $do_hard_later = null; // Prevent this action from running before everyone has registered their rewrites. if ( ! did_action( 'wp_loaded' ) ) { add_action( 'wp_loaded', array( $this, 'flush_rules' ) ); $do_hard_later = ( isset( $do_hard_later ) ) ? $do_hard_later || $hard : $hard; return; } if ( isset( $do_hard_later ) ) { $hard = $do_hard_later; unset( $do_hard_later ); } $this->refresh_rewrite_rules(); /** * Filters whether a "hard" rewrite rule flush should be performed when requested. * * A "hard" flush updates .htaccess (Apache) or web.config (IIS). * * @since 3.7.0 * * @param bool $hard Whether to flush rewrite rules "hard". Default true. */ if ( ! $hard || ! apply_filters( 'flush_rewrite_rules_hard', true ) ) { return; } if ( function_exists( 'save_mod_rewrite_rules' ) ) { save_mod_rewrite_rules(); } if ( function_exists( 'iis7_save_url_rewrite_rules' ) ) { iis7_save_url_rewrite_rules(); } } /** * Sets up the object's properties. * * The 'use_verbose_page_rules' object property will be set to true if the * permalink structure begins with one of the following: '%postname%', '%category%', * '%tag%', or '%author%'. * * @since 1.5.0 */ public function init() { $this->extra_rules = array(); $this->non_wp_rules = array(); $this->endpoints = array(); $this->permalink_structure = get_option( 'permalink_structure' ); $this->front = substr( $this->permalink_structure, 0, strpos( $this->permalink_structure, '%' ) ); $this->root = ''; if ( $this->using_index_permalinks() ) { $this->root = $this->index . '/'; } unset( $this->author_structure ); unset( $this->date_structure ); unset( $this->page_structure ); unset( $this->search_structure ); unset( $this->feed_structure ); unset( $this->comment_feed_structure ); $this->use_trailing_slashes = str_ends_with( $this->permalink_structure, '/' ); // Enable generic rules for pages if permalink structure doesn't begin with a wildcard. if ( preg_match( '/^[^%]*%(?:postname|category|tag|author)%/', $this->permalink_structure ) ) { $this->use_verbose_page_rules = true; } else { $this->use_verbose_page_rules = false; } } /** * Sets the main permalink structure for the site. * * Will update the 'permalink_structure' option, if there is a difference * between the current permalink structure and the parameter value. Calls * WP_Rewrite::init() after the option is updated. * * Fires the {@see 'permalink_structure_changed'} action once the init call has * processed passing the old and new values * * @since 1.5.0 * * @param string $permalink_structure Permalink structure. */ public function set_permalink_structure( $permalink_structure ) { if ( $this->permalink_structure !== $permalink_structure ) { $old_permalink_structure = $this->permalink_structure; update_option( 'permalink_structure', $permalink_structure ); $this->init(); /** * Fires after the permalink structure is updated. * * @since 2.8.0 * * @param string $old_permalink_structure The previous permalink structure. * @param string $permalink_structure The new permalink structure. */ do_action( 'permalink_structure_changed', $old_permalink_structure, $permalink_structure ); } } /** * Sets the category base for the category permalink. * * Will update the 'category_base' option, if there is a difference between * the current category base and the parameter value. Calls WP_Rewrite::init() * after the option is updated. * * @since 1.5.0 * * @param string $category_base Category permalink structure base. */ public function set_category_base( $category_base ) { if ( get_option( 'category_base' ) !== $category_base ) { update_option( 'category_base', $category_base ); $this->init(); } } /** * Sets the tag base for the tag permalink. * * Will update the 'tag_base' option, if there is a difference between the * current tag base and the parameter value. Calls WP_Rewrite::init() after * the option is updated. * * @since 2.3.0 * * @param string $tag_base Tag permalink structure base. */ public function set_tag_base( $tag_base ) { if ( get_option( 'tag_base' ) !== $tag_base ) { update_option( 'tag_base', $tag_base ); $this->init(); } } /** * Constructor - Calls init(), which runs setup. * * @since 1.5.0 */ public function __construct() { $this->init(); } } rue ); } /** * Checks whether a given taxonomy is public. * * @since 4.2.2 * * @param string $taxonomy The taxonomy. * @return bool Whether the taxonomy is public. */ public function isTaxonomyPublic( $taxonomy ) { $publicTaxonomies = $this->getPublicTaxonomies( true ); return in_array( $taxonomy, $publicTaxonomies, true ); } /** * Returns noindexed object types of a given parent type. * * @since 4.0.0 * * @param string $type The parent object type ("postTypes" or "taxonomies"). * @return array A list of noindexed objects types. */ private function getNoindexedObjects( $type ) { $noindexed = []; foreach ( aioseo()->dynamicOptions->searchAppearance->$type->all() as $name => $object ) { if ( ! $object['show'] || ( $object['advanced']['robotsMeta'] && ! $object['advanced']['robotsMeta']['default'] && $object['advanced']['robotsMeta']['noindex'] ) ) { $noindexed[] = $name; } } return $noindexed; } /** * Returns all categories for a post. * * @since 4.1.4 * * @param int $postId The post ID. * @return array The category names. */ public function getAllCategories( $postId = 0 ) { $names = []; $categories = get_the_category( $postId ); if ( $categories && count( $categories ) ) { foreach ( $categories as $category ) { $names[] = aioseo()->helpers->internationalize( $category->name ); } } return $names; } /** * Returns all tags for a post. * * @since 4.1.4 * * @param int $postId The post ID. * @return array $names The tag names. */ public function getAllTags( $postId = 0 ) { $names = []; $tags = get_the_tags( $postId ); if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) { foreach ( $tags as $tag ) { if ( ! empty( $tag->name ) ) { $names[] = aioseo()->helpers->internationalize( $tag->name ); } } } return $names; } /** * Loads the translations for a given domain. * * @since 4.1.4 * * @return void */ public function loadTextDomain( $domain ) { if ( ! is_user_logged_in() ) { return; } // Unload the domain in case WordPress has enqueued the translations for the site language instead of profile language. // Reloading the text domain will otherwise not override the existing loaded translations. unload_textdomain( $domain ); $mofile = $domain . '-' . get_user_locale() . '.mo'; load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ); } /** * Get the page builder the given Post ID was built with. * * @since 4.1.7 * * @param int $postId The Post ID. * @return bool|string The page builder or false if not built with page builders. */ public function getPostPageBuilderName( $postId ) { foreach ( aioseo()->standalone->pageBuilderIntegrations as $integration => $pageBuilder ) { if ( $pageBuilder->isBuiltWith( $postId ) ) { return $integration; } } return false; } /** * Get the edit link for the given Post ID. * * @since 4.3.1 * * @param int $postId The Post ID. * @return bool|string The edit link or false if not built with page builders. */ public function getPostEditLink( $postId ) { $pageBuilder = $this->getPostPageBuilderName( $postId ); if ( ! empty( $pageBuilder ) ) { return aioseo()->standalone->pageBuilderIntegrations[ $pageBuilder ]->getEditUrl( $postId ); } return get_edit_post_link( $postId ); } /** * Checks if the current user can edit posts of the given post type. * * @since 4.1.9 * * @param string $postType The name of the post type. * @return bool Whether the user can edit posts of the given post type. */ public function canEditPostType( $postType ) { $capabilities = $this->getPostTypeCapabilities( $postType ); return current_user_can( $capabilities['edit_posts'] ); } /** * Returns a list of capabilities for the given post type. * * @since 4.1.9 * * @param string $postType The name of the post type. * @return array The capabilities. */ public function getPostTypeCapabilities( $postType ) { static $capabilities = []; if ( isset( $capabilities[ $postType ] ) ) { return $capabilities[ $postType ]; } $postTypeObject = get_post_type_object( $postType ); if ( ! is_a( $postTypeObject, 'WP_Post_Type' ) ) { $capabilities[ $postType ] = []; return $capabilities[ $postType ]; } $capabilityType = $postTypeObject->capability_type; if ( ! is_array( $capabilityType ) ) { $capabilityType = [ $capabilityType, $capabilityType . 's' ]; } // Singular base for meta capabilities, plural base for primitive capabilities. list( $singularBase, $pluralBase ) = $capabilityType; $capabilities[ $postType ] = [ 'edit_post' => 'edit_' . $singularBase, 'read_post' => 'read_' . $singularBase, 'delete_post' => 'delete_' . $singularBase, 'edit_posts' => 'edit_' . $pluralBase, 'edit_others_posts' => 'edit_others_' . $pluralBase, 'delete_posts' => 'delete_' . $pluralBase, 'publish_posts' => 'publish_' . $pluralBase, 'read_private_posts' => 'read_private_' . $pluralBase, ]; return $capabilities[ $postType ]; } /** * Checks if the current user can edit terms of the given taxonomy. * * @since 4.1.9 * * @param string $taxonomy The name of the taxonomy. * @return bool Whether the user can edit posts of the given taxonomy. */ public function canEditTaxonomy( $taxonomy ) { $capabilities = $this->getTaxonomyCapabilities( $taxonomy ); return current_user_can( $capabilities['edit_terms'] ); } /** * Returns a list of capabilities for the given taxonomy. * * @since 4.1.9 * * @param string $taxonomy The name of the taxonomy. * @return array The capabilities. */ public function getTaxonomyCapabilities( $taxonomy ) { static $capabilities = []; if ( isset( $capabilities[ $taxonomy ] ) ) { return $capabilities[ $taxonomy ]; } $taxonomyObject = get_taxonomy( $taxonomy ); if ( ! is_a( $taxonomyObject, 'WP_Taxonomy' ) ) { $capabilities[ $taxonomy ] = []; return $capabilities[ $taxonomy ]; } $capabilities[ $taxonomy ] = (array) $taxonomyObject->cap; return $capabilities[ $taxonomy ]; } /** * Returns the charset for the site. * * @since 4.2.3 * * @return string The name of the charset. */ public function getCharset() { static $charset = null; if ( null !== $charset ) { return $charset; } $charset = get_option( 'blog_charset' ); $charset = $charset ? $charset : 'UTF-8'; return $charset; } /** * Returns the given data as JSON. * We temporarily change the floating point precision in order to prevent rounding errors. * Otherwise e.g. 4.9 could be output as 4.90000004. * * @since 4.2.7 * * @param mixed $data The data. * @param int $flags The flags. * @return string The JSON output. */ public function wpJsonEncode( $data, $flags = 0 ) { $originalPrecision = false; $originalSerializePrecision = false; if ( version_compare( PHP_VERSION, '7.1', '>=' ) ) { $originalPrecision = ini_get( 'precision' ); $originalSerializePrecision = ini_get( 'serialize_precision' ); ini_set( 'precision', 17 ); ini_set( 'serialize_precision', -1 ); } $json = wp_json_encode( $data, $flags ); if ( version_compare( PHP_VERSION, '7.1', '>=' ) ) { ini_set( 'precision', $originalPrecision ); ini_set( 'serialize_precision', $originalSerializePrecision ); } return $json; } /** * Returns the post title or a placeholder if there isn't one. * * @since 4.3.0 * * @param int $postId The post ID. * @return string The post title. */ public function getPostTitle( $postId ) { static $titles = []; if ( isset( $titles[ $postId ] ) ) { return $titles[ $postId ]; } $post = aioseo()->helpers->getPost( $postId ); if ( ! is_a( $post, 'WP_Post' ) ) { $titles[ $postId ] = __( '(no title)' ); // phpcs:ignore AIOSEO.Wp.I18n.MissingArgDomain return $titles[ $postId ]; } $title = $post->post_title; $title = $title ? $title : __( '(no title)' ); // phpcs:ignore AIOSEO.Wp.I18n.MissingArgDomain $titles[ $postId ] = aioseo()->helpers->decodeHtmlEntities( $title ); return $titles[ $postId ]; } /** * Checks whether the post status should be considered viewable. * This function is a copy of the WordPress core function is_post_status_viewable() which was introduced in WP 5.7. * * @since 4.5.0 * * @param string|\stdClass $postStatus The post status name or object. * @return bool Whether the post status is viewable. */ public function isPostStatusViewable( $postStatus ) { if ( is_scalar( $postStatus ) ) { $postStatus = get_post_status_object( $postStatus ); if ( ! $postStatus ) { return false; } } if ( ! is_object( $postStatus ) || $postStatus->internal || $postStatus->protected ) { return false; } return $postStatus->publicly_queryable || ( $postStatus->_builtin && $postStatus->public ); } /** * Checks whether the given post is publicly viewable. * This function is a copy of the WordPress core function is_post_publicly_viewable() which was introduced in WP 5.7. * * @since 4.5.0 * * @param int|\WP_Post $post Optional. Post ID or post object. Defaults to global $post. * @return boolean Whether the post is publicly viewable or not. */ public function isPostPubliclyViewable( $post = null ) { $post = get_post( $post ); if ( empty( $post ) ) { return false; } $postType = get_post_type( $post ); $postStatus = get_post_status( $post ); return is_post_type_viewable( $postType ) && $this->isPostStatusViewable( $postStatus ); } /** * Only register a legacy widget if the WP version is lower than 5.8 or the widget is being used. * The "Block-based Widgets Editor" was released in WP 5.8, so for WP versions below 5.8 it's okay to register them. * The main purpose here is to avoid blocks and widgets with the same name to be displayed on the Customizer, * like e.g. the "Breadcrumbs" Block and Widget. * * @since 4.3.9 * * @param string $idBase The base ID of a widget created by extending WP_Widget. * @return bool Whether the legacy widget can be registered. */ public function canRegisterLegacyWidget( $idBase ) { global $wp_version; if ( version_compare( $wp_version, '5.8', '<' ) || is_active_widget( false, false, $idBase ) || aioseo()->standalone->pageBuilderIntegrations['elementor']->isPluginActive() ) { return true; } return false; } }