************* HELP TEXT RELATED ************************** */ /** * Create a variable help text table. * * @param string $type Either 'basic' or 'advanced'. * * @return string Help text table. */ private static function create_variable_help_table( $type ) { if ( ! in_array( $type, array( 'basic', 'advanced' ), true ) ) { return ''; } $table = ' '; foreach ( self::$help_texts[ $type ] as $replacement_variable ) { $table .= ' '; } $table .= '
' . esc_html__( 'Label', 'wordpress-seo' ) . ' ' . esc_html__( 'Variable', 'wordpress-seo' ) . ' ' . esc_html__( 'Description', 'wordpress-seo' ) . '
' . esc_html( $replacement_variable->get_label() ) . ' %%' . esc_html( $replacement_variable->get_variable() ) . '%% ' . esc_html( $replacement_variable->get_description() ) . '
'; return $table; } /** * Create the help text table for the basic variables for use in a help tab. * * @return string */ public static function get_basic_help_texts() { return self::create_variable_help_table( 'basic' ); } /** * Create the help text table for the advanced variables for use in a help tab. * * @return string */ public static function get_advanced_help_texts() { return self::create_variable_help_table( 'advanced' ); } /** * Set the help text for a user/plugin/theme defined extra variable. * * @param string $type Type of variable: 'basic' or 'advanced'. * @param WPSEO_Replacement_Variable $replacement_variable The replacement variable to register. */ private static function register_help_text( $type, WPSEO_Replacement_Variable $replacement_variable ) { $identifier = $replacement_variable->get_variable(); if ( ( is_string( $type ) && in_array( $type, array( 'basic', 'advanced' ), true ) ) && ( $identifier !== '' && ! isset( self::$help_texts[ $type ][ $identifier ] ) ) ) { self::$help_texts[ $type ][ $identifier ] = $replacement_variable; } } /** * Generates a list of replacement variables based on the help texts. * * @return array List of replace vars. */ public function get_replacement_variables_list() { self::setup_statics_once(); $replacement_variables = array_merge( $this->get_replacement_variables(), WPSEO_Custom_Fields::get_custom_fields(), WPSEO_Custom_Taxonomies::get_custom_taxonomies() ); return array_map( array( $this, 'format_replacement_variable' ), $replacement_variables ); } /** * Creates a merged associative array of both the basic and advanced help texts. * * @return array Array with the replacement variables. */ private function get_replacement_variables() { $help_texts = array_merge( self::$help_texts['basic'], self::$help_texts['advanced'] ); return array_filter( array_keys( $help_texts ), array( $this, 'is_not_prefixed' ) ); } /** * Checks whether the replacement variable contains a `ct_` or `cf_` prefix, because they follow different logic. * * @param string $replacement_variable The replacement variable. * * @return bool True when the replacement variable is not prefixed. */ private function is_not_prefixed( $replacement_variable ) { $prefixes = array( 'cf_', 'ct_' ); $prefix = $this->get_prefix( $replacement_variable ); return ! in_array( $prefix, $prefixes, true ); } /** * Strip the prefix from a replacement variable name. * * @param string $replacement_variable The replacement variable. * * @return string The replacement variable name without the prefix. */ private function strip_prefix( $replacement_variable ) { return substr( $replacement_variable, 3 ); } /** * Gets the prefix from a replacement variable name. * * @param string $replacement_variable The replacement variable. * * @return string The prefix of the replacement variable. */ private function get_prefix( $replacement_variable ) { return substr( $replacement_variable, 0, 3 ); } /** * Strips 'desc_' if present, and appends ' description' at the end. * * @param string $label The replacement variable. * * @return string The altered replacement variable name. */ private function handle_description( $label ) { if ( strpos( $label, 'desc_' ) === 0 ) { return substr( $label, 5 ) . ' description'; } return $label; } /** * Creates a label for prefixed replacement variables that matches the format in the editors. * * @param string $replacement_variable The replacement variable. * * @return string The replacement variable label. */ private function get_label( $replacement_variable ) { $prefix = $this->get_prefix( $replacement_variable ); if ( $prefix === 'cf_' ) { return $this->strip_prefix( $replacement_variable ) . ' (custom field)'; } if ( $prefix === 'ct_' ) { $label = $this->strip_prefix( $replacement_variable ); $label = $this->handle_description( $label ); return ucfirst( $label . ' (custom taxonomy)' ); } if ( $prefix === 'pt_' ) { if ( $replacement_variable === 'pt_single' ) { return 'Post type (singular)'; } return 'Post type (plural)'; } return ''; } /** * Formats the replacement variables. * * @param string $replacement_variable The replacement variable to format. * * @return array The formatted replacement variable. */ private function format_replacement_variable( $replacement_variable ) { return array( 'name' => $replacement_variable, 'value' => '', 'label' => $this->get_label( $replacement_variable ), ); } /** * Retrieves the custom field names as an array. * * @link WordPress core: wp-admin/includes/template.php. Reused query from it. * * @return array The custom fields. */ private function get_custom_fields() { global $wpdb; /** * Filters the number of custom fields to retrieve for the drop-down * in the Custom Fields meta box. * * @since 2.1.0 * * @param int $limit Number of custom fields to retrieve. Default 30. */ $limit = apply_filters( 'postmeta_form_limit', 30 ); $sql = "SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE meta_key NOT BETWEEN '_' AND '_z' HAVING meta_key NOT LIKE %s ORDER BY meta_key LIMIT %d"; $fields = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%', $limit ) ); if ( is_array( $fields ) ) { return array_map( array( $this, 'add_custom_field_prefix' ), $fields ); } return array(); } /** * Adds the cf_ prefix to a field. * * @param string $field The field to prefix. * * @return string The prefixed field. */ private function add_custom_field_prefix( $field ) { return 'cf_' . $field; } /** * Gets the names of the custom taxonomies, prepends 'ct_' and 'ct_desc', and returns them in an array. * * @return array The custom taxonomy prefixed names. */ private function get_custom_taxonomies() { $args = array( 'public' => true, '_builtin' => false, ); $output = 'names'; $operator = 'and'; $custom_taxonomies = get_taxonomies( $args, $output, $operator ); if ( is_array( $custom_taxonomies ) ) { $ct_replace_vars = array(); foreach ( $custom_taxonomies as $custom_taxonomy ) { array_push( $ct_replace_vars, 'ct_' . $custom_taxonomy, 'ct_desc_' . $custom_taxonomy ); } return $ct_replace_vars; } return array(); } /** * Set/translate the help texts for the WPSEO standard basic variables. */ private static function set_basic_help_texts() { /* translators: %s: wp_title() function. */ $separator_description = __( 'The separator defined in your theme\'s %s tag.', 'wordpress-seo' ); $separator_description = sprintf( $separator_description, // 'wp_title()' 'wp_title()' ); $replacement_variables = array( new WPSEO_Replacement_Variable( 'date', __( 'Date', 'wordpress-seo' ), __( 'Replaced with the date of the post/page', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'title', __( 'Title', 'wordpress-seo' ), __( 'Replaced with the title of the post/page', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'parent_title', __( 'Parent title', 'wordpress-seo' ), __( 'Replaced with the title of the parent page of the current page', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'archive_title', __( 'Archive title', 'wordpress-seo' ), __( 'Replaced with the normal title for an archive generated by WordPress', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'sitename', __( 'Site title', 'wordpress-seo' ), __( 'The site\'s name', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'sitedesc', __( 'Tagline', 'wordpress-seo' ), __( 'The site\'s tagline', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'excerpt', __( 'Excerpt', 'wordpress-seo' ), __( 'Replaced with the post/page excerpt (or auto-generated if it does not exist)', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'excerpt_only', __( 'Excerpt only', 'wordpress-seo' ), __( 'Replaced with the post/page excerpt (without auto-generation)', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'tag', __( 'Tag', 'wordpress-seo' ), __( 'Replaced with the current tag/tags', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'category', __( 'Category', 'wordpress-seo' ), __( 'Replaced with the post categories (comma separated)', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'primary_category', __( 'Primary category', 'wordpress-seo' ), __( 'Replaced with the primary category of the post/page', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'category_description', __( 'Category description', 'wordpress-seo' ), __( 'Replaced with the category description', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'tag_description', __( 'Tag description', 'wordpress-seo' ), __( 'Replaced with the tag description', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'term_description', __( 'Term description', 'wordpress-seo' ), __( 'Replaced with the term description', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'term_title', __( 'Term title', 'wordpress-seo' ), __( 'Replaced with the term name', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'searchphrase', __( 'Search phrase', 'wordpress-seo' ), __( 'Replaced with the current search phrase', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'sep', __( 'Separator', 'wordpress-seo' ), $separator_description ), ); foreach ( $replacement_variables as $replacement_variable ) { self::register_help_text( 'basic', $replacement_variable ); } } /** * Set/translate the help texts for the WPSEO standard advanced variables. */ private static function set_advanced_help_texts() { $replacement_variables = array( new WPSEO_Replacement_Variable( 'pt_single', __( 'Post type (singular)', 'wordpress-seo' ), __( 'Replaced with the content type single label', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'pt_plural', __( 'Post type (plural)', 'wordpress-seo' ), __( 'Replaced with the content type plural label', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'modified', __( 'Modified', 'wordpress-seo' ), __( 'Replaced with the post/page modified time', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'id', __( 'ID', 'wordpress-seo' ), __( 'Replaced with the post/page ID', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'name', __( 'Name', 'wordpress-seo' ), __( 'Replaced with the post/page author\'s \'nicename\'', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'user_description', __( 'User description', 'wordpress-seo' ), __( 'Replaced with the post/page author\'s \'Biographical Info\'', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'page', __( 'Page number', 'wordpress-seo' ), __( 'Replaced with the current page number with context (i.e. page 2 of 4)', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'pagetotal', __( 'Pagetotal', 'wordpress-seo' ), __( 'Replaced with the current page total', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'pagenumber', __( 'Pagenumber', 'wordpress-seo' ), __( 'Replaced with the current page number', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'caption', __( 'Caption', 'wordpress-seo' ), __( 'Attachment caption', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'focuskw', __( 'Focus keyword', 'wordpress-seo' ), __( 'Replaced with the posts focus keyphrase', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'term404', __( 'Term404', 'wordpress-seo' ), __( 'Replaced with the slug which caused the 404', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'cf_', ' ' . __( '(custom field)', 'wordpress-seo' ), __( 'Replaced with a posts custom field value', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'ct_', ' ' . __( '(custom taxonomy)', 'wordpress-seo' ), __( 'Replaced with a posts custom taxonomies, comma separated.', 'wordpress-seo' ) ), new WPSEO_Replacement_Variable( 'ct_desc_', ' ' . __( 'description (custom taxonomy)', 'wordpress-seo' ), __( 'Replaced with a custom taxonomies description', 'wordpress-seo' ) ), ); foreach ( $replacement_variables as $replacement_variable ) { self::register_help_text( 'advanced', $replacement_variable ); } } /* *********************** GENERAL HELPER METHODS ************************** */ /** * Remove the '%%' delimiters from a variable string. * * @param string $string Variable string to be cleaned. * * @return string */ private static function remove_var_delimiter( $string ) { return trim( $string, '%' ); } /** * Add the '%%' delimiters to a variable string. * * @param string $string Variable string to be delimited. * * @return string */ private static function add_var_delimiter( $string ) { return '%%' . $string . '%%'; } /** * Retrieve a post's terms, comma delimited. * * @param int $id ID of the post to get the terms for. * @param string $taxonomy The taxonomy to get the terms for this post from. * @param bool $return_single If true, return the first term. * * @return string Either a single term or a comma delimited string of terms. */ public function get_terms( $id, $taxonomy, $return_single = false ) { $output = ''; // If we're on a specific tag, category or taxonomy page, use that. if ( is_category() || is_tag() || is_tax() ) { $term = $GLOBALS['wp_query']->get_queried_object(); $output = $term->name; } elseif ( ! empty( $id ) && ! empty( $taxonomy ) ) { $terms = get_the_terms( $id, $taxonomy ); if ( is_array( $terms ) && $terms !== array() ) { foreach ( $terms as $term ) { if ( $return_single ) { $output = $term->name; break; } else { $output .= $term->name . ', '; } } $output = rtrim( trim( $output ), ',' ); } } unset( $terms, $term ); /** * Allows filtering of the terms list used to replace %%category%%, %%tag%% and %%ct_%% variables. * * @api string $output Comma-delimited string containing the terms. * @api string $taxonomy The taxonomy of the terms. */ return apply_filters( 'wpseo_terms', $output, $taxonomy ); } } /* End of class WPSEO_Replace_Vars */