te that only replacement variables that are surrounded by a space on either side are captured, * and that the replacement is a single space. */ return preg_replace( '`(\s%%[^\s%]+%%\s)+`iu', ' ', $replaced ); } /** * Returns the content from a post, in which shortcodes have been replaced. * * @param WP_Post $post_with_shortcodes An WP post. * * @return string The content of the post with all shortcodes in the post content processed. */ public function process_shortcodes( $post_with_shortcodes ) { global $post; /* * Set the global $post to be the post in this iteration. * This is required for post-specific shortcodes that reference the global post. */ // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly. $post = $post_with_shortcodes; // Set up WordPress data for this post, outside of "the_loop". setup_postdata( $post ); $content = do_shortcode( $post->post_content ); wp_reset_postdata(); return $content; } /** * Builds the data for a post. * * @param WP_Post $post A WordPress post. * * @return array The enriched post. */ protected function build_post_data( $post ) { $meta_data = array(); foreach ( $this->get_meta_keys() as $meta_key => $meta_info ) { $meta_data_for_key = $this->get_meta_value_wrapper( $meta_key, $post->ID ); $post_type = $post->post_type; /* * If there is no meta_data for the current key, check for default settings * * For title we need to check if there is a modified SEO-title available and save it. * If the SEO title was not modified, we check if the user specified a default SEO title for this content type. * Otherwise we use a default title '%%title%% %%sep%% %%sitename%%' (WPSEO_Frontend::$default_title). * * If the meta description was not modified, we check if the user specified a default meta description for this content type. */ if ( $meta_data_for_key === '' ) { switch ( $meta_key ) { case 'title': $meta_data_for_key = $this->apply_default_title( $post_type ); break; case 'metadesc': $meta_data_for_key = $this->get_options_wrapper( 'metadesc-' . $post_type, '' ); break; } } // Get the name we use as a human readable meta_data label in the results. $meta_key_name = $meta_info['name']; // Apply json decoder to meta keys that are arrays (e.g., related keywords). if ( $meta_info['should_decode'] ) { $meta_data[ $meta_key_name ] = json_decode( $meta_data_for_key ); } else { $meta_data[ $meta_key_name ] = $meta_data_for_key; if ( $meta_info['should_replace_vars'] ) { $meta_data[ $meta_key_name ] = $this->process_replacevars( $meta_data_for_key, $post ); } } } return array( 'ID' => $post->ID, 'post_content' => $this->process_shortcodes( $post ), 'meta' => $meta_data, ); } }