/** * @covers WPSEO_Meta::set_value() */ public function test_set_value() { // create and go to post $post_id = $this->factory->post->create(); $this->go_to(get_permalink($post_id)); WPSEO_Meta::set_value('test_set_value_key', 'test_set_value_value', $post_id); $this->assertEquals('test_set_value_value', get_post_meta($post_id, WPSEO_Meta::$meta_prefix . 'test_set_value_key', true)); }
/** * Saving the new linkdex score for given post */ public function save_score() { check_ajax_referer('wpseo_recalculate', 'nonce'); $scores = filter_input(INPUT_POST, 'scores', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); foreach ($scores as $score) { WPSEO_Meta::set_value('linkdex', $score['score'], $score['post_id']); } wp_die(); }
/** * Getting the wpSEO robot value and map this to Yoast SEO values. * * @param integer $post_id The post id of the current post. */ private function import_post_robot($post_id) { $wpseo_robots = get_post_meta($post_id, '_wpseo_edit_robots', true); // Does the value exists in our mapping. if ($robot_value = $this->get_robot_value($wpseo_robots)) { // Saving the new meta values for Yoast SEO. WPSEO_Meta::set_value($robot_value['index'], 'meta-robots-noindex', $post_id); WPSEO_Meta::set_value($robot_value['follow'], 'meta-robots-nofollow', $post_id); } $this->delete_post_robot($post_id); }
/** * @covers WPSEO_GooglePlus::description */ public function test_description() { self::$class_instance->description(); $this->expectOutput(''); // create and go to post $post_id = $this->factory->post->create(); $this->go_to(get_permalink($post_id)); // should be empty, didn't set google-plus-description self::$class_instance->description(); $this->expectOutput(''); // set meta $description = 'Google description'; WPSEO_Meta::set_value('google-plus-description', $description, $post_id); // test output $expected = '<meta itemprop="description" content="' . $description . '">' . "\n"; self::$class_instance->description(); $this->expectOutput($expected); }
/** * Check and, if applicable, update video details for a post * * @since 0.1 * * @param object $post The post to check and possibly update the video details for. * @param boolean $echo Whether or not to echo the performed actions. * * @return mixed $vid The video array that was just stored, or "none" if nothing was stored * or false if not applicable. */ public function update_video_post_meta($post, $echo = false) { global $wp_query; if (is_numeric($post)) { $post = get_post($post); } if (!isset($post->ID)) { return false; } $options = array_merge(WPSEO_Options::get_all(), get_option('wpseo_video')); if (!is_array($options['videositemap_posttypes']) || $options['videositemap_posttypes'] === array()) { return false; } if (!in_array($post->post_type, $options['videositemap_posttypes'])) { return false; } $_GLOBALS['post'] = $post; // @todo Que ? Overwritting the global post object seems like a bad idea, quite apart from that you'd want $GLOBALS without the underscore to do so $old_vid = array(); if (!isset($_POST['force'])) { $old_vid = WPSEO_Meta::get_value('video_meta', $post->ID); } $title = WPSEO_Meta::get_value('title', $post->ID); if ((!is_string($title) || $title === '') && isset($options['title-' . $post->post_type]) && $options['title-' . $post->post_type] !== '') { $title = wpseo_replace_vars($options['title-' . $post->post_type], (array) $post); } elseif ((!is_string($title) || $title === '') && (!isset($options['title-' . $post->post_type]) || $options['title-' . $post->post_type] === '')) { $title = wpseo_replace_vars('%%title%% - %%sitename%%', (array) $post); } if (!is_string($title) || $title === '') { $title = $post->post_title; } $vid = array(); // @todo [JRF->Yoast] Verify if this is really what we want. What about non-hierarchical custom post types ? and are we adjusting the main query output now ? could this cause bugs for others ? if ($post->post_type == 'post') { $wp_query->is_single = true; $wp_query->is_page = false; } else { $wp_query->is_single = false; $wp_query->is_page = true; } $vid['post_id'] = $post->ID; $vid['title'] = htmlspecialchars($title); $vid['publication_date'] = mysql2date('Y-m-d\\TH:i:s+00:00', $post->post_date_gmt); $vid['description'] = WPSEO_Meta::get_value('metadesc', $post->ID); if (!is_string($vid['description']) || $vid['description'] === '') { $vid['description'] = esc_attr(preg_replace('`\\s+`', ' ', wp_html_excerpt($this->strip_shortcodes($post->post_content), 300))); } $vid = $this->index_content($post->post_content, $vid, $old_vid, $post); if ('none' != $vid) { // Shouldn't be needed, but just in case if (isset($vid['__add_to_content'])) { unset($vid['__add_to_content']); } if (!isset($vid['thumbnail_loc']) || empty($vid['thumbnail_loc'])) { $img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); if (strpos($img[0], 'http') !== 0) { $vid['thumbnail_loc'] = get_site_url(null, $img[0]); } else { $vid['thumbnail_loc'] = $img[0]; } } // Grab the meta data from the post if (isset($_POST['yoast_wpseo_videositemap-category']) && !empty($_POST['yoast_wpseo_videositemap-category'])) { $vid['category'] = sanitize_text_field($_POST['yoast_wpseo_videositemap-category']); } else { $cats = wp_get_object_terms($post->ID, 'category', array('fields' => 'names')); if (isset($cats[0])) { $vid['category'] = $cats[0]; } unset($cats); } $tags = wp_get_object_terms($post->ID, 'post_tag', array('fields' => 'names')); if (isset($_POST['yoast_wpseo_videositemap-tags']) && !empty($_POST['yoast_wpseo_videositemap-tags'])) { $extra_tags = explode(',', sanitize_text_field($_POST['yoast_wpseo_videositemap-tags'])); $tags = array_merge($extra_tags, $tags); } $tag = array(); if (is_array($tags)) { foreach ($tags as $t) { $tag[] = $t; } } elseif (isset($cats[0])) { $tag[] = $cats[0]->name; } $focuskw = WPSEO_Meta::get_value('focuskw', $post->ID); if (!empty($focuskw)) { $tag[] = $focuskw; } $vid['tag'] = $tag; if ($echo) { echo 'Updated <a href="' . esc_url(add_query_arg(array('p' => $post->ID), home_url())) . '">' . esc_html($post->post_title) . '</a> - ' . esc_html($vid['type']) . '<br/>'; } } WPSEO_Meta::set_value('video_meta', $vid, $post->ID); //echo '<pre>' . print_r( $_POST, true ) . '</pre>'; return $vid; }
/** * Import HeadSpace SEO settings */ public function import_headspace() { global $wpdb; WPSEO_Meta::replace_meta('_headspace_description', WPSEO_Meta::$meta_prefix . 'metadesc', $this->replace); WPSEO_Meta::replace_meta('_headspace_keywords', WPSEO_Meta::$meta_prefix . 'metakeywords', $this->replace); WPSEO_Meta::replace_meta('_headspace_page_title', WPSEO_Meta::$meta_prefix . 'title', $this->replace); /** * @todo [JRF => whomever] verify how headspace sets these metas ( 'noindex', 'nofollow', 'noarchive', 'noodp', 'noydir' ) * and if the values saved are concurrent with the ones we use (i.e. 0/1/2) */ WPSEO_Meta::replace_meta('_headspace_noindex', WPSEO_Meta::$meta_prefix . 'meta-robots-noindex', $this->replace); WPSEO_Meta::replace_meta('_headspace_nofollow', WPSEO_Meta::$meta_prefix . 'meta-robots-nofollow', $this->replace); /* * @todo - [JRF => whomever] check if this can be done more efficiently by querying only the meta table * possibly directly changing it using concat on the existing values */ $posts = $wpdb->get_results("SELECT ID FROM {$wpdb->posts}"); if (is_array($posts) && $posts !== array()) { foreach ($posts as $post) { $custom = get_post_custom($post->ID); $robotsmeta_adv = ''; if (isset($custom['_headspace_noarchive'])) { $robotsmeta_adv .= 'noarchive,'; } if (isset($custom['_headspace_noodp'])) { $robotsmeta_adv .= 'noodp,'; } $robotsmeta_adv = preg_replace('`,$`', '', $robotsmeta_adv); WPSEO_Meta::set_value('meta-robots-adv', $robotsmeta_adv, $post->ID); } } if ($this->replace) { // We no longer use noydir, but we remove the meta key as it's unneeded. $hs_meta = array('noarchive', 'noodp', 'noydir'); foreach ($hs_meta as $meta) { delete_post_meta_by_key('_headspace_' . $meta); } unset($hs_meta, $meta); } $this->set_msg(__('HeadSpace2 data successfully imported', 'wordpress-seo')); }
/** * @covers WPSEO_OpenGraph::description */ public function test_description_single_post() { $expected_opengraph_description = 'This is with a opengraph-description'; $expected_meta_description = 'This is with a meta-description'; $expected_excerpt = 'Post excerpt 1'; // Creates the post $post_id = $this->factory->post->create(); $this->go_to(get_permalink($post_id)); // Checking opengraph-description and after obtaining its value, reset the meta value for it WPSEO_Meta::set_value('opengraph-description', $expected_opengraph_description, $post_id); $opengraph_description = self::$class_instance->description(false); WPSEO_Meta::set_value('opengraph-description', '', $post_id); $this->assertEquals($expected_opengraph_description, $opengraph_description); // Checking meta-description and after obtaining its value, reset the meta value for it WPSEO_Meta::set_value('metadesc', $expected_meta_description, $post_id); $meta_description = self::$class_instance->description(false); WPSEO_Meta::set_value('metadesc', '', $post_id); $this->assertEquals($expected_meta_description, $meta_description); // Checking with the excerpt $excerpt = self::$class_instance->description(false); $this->assertEquals($expected_excerpt, $excerpt); }
/** * @covers WPSEO_Frontend::canonical */ public function test_canonical_single_post_override() { // create and go to post $post_id = $this->factory->post->create(); // test default canonical $expected = get_permalink($post_id); // test manual override while using no override $meta_canon = 'http://canonic.al'; WPSEO_Meta::set_value('canonical', $meta_canon, $post_id); $this->go_to(get_permalink($post_id)); $this->assertEquals($expected, self::$class_instance->canonical(false, false, true)); // test manual override $this->assertEquals($meta_canon, self::$class_instance->canonical(false)); }
/** * Save a custom meta value * * @deprecated 1.5.0 * @deprecated use WPSEO_Meta::set_value() or just use update_post_meta() * @see WPSEO_Meta::set_value() * * @param string $meta_key the meta to change * @param mixed $meta_value the value to set the meta to * @param int $post_id the ID of the post to change the meta for. * * @return bool whether the value was changed */ function wpseo_set_value($meta_key, $meta_value, $post_id) { _deprecated_function(__FUNCTION__, 'WPSEO 1.5.0', 'WPSEO_Meta::set_value()'); return WPSEO_Meta::set_value($meta_key, $meta_value, $post_id); }
/** * Save the score. * * @param array $score The score to save. */ protected function save_score(array $score) { WPSEO_Meta::set_value('linkdex', $score['score'], $score['item_id']); }
/** * @covers WPSEO_Twitter::image() */ public function test_meta_value_image() { // create and go to post $post_id = $this->factory->post->create(); $this->go_to(get_permalink($post_id)); // test wpseo meta value $image_url = 'http://url-singular-meta-image.jpg'; WPSEO_Meta::set_value('twitter-image', $image_url, $post_id); $expected = $this->get_expected_image_output($image_url); self::$class_instance->image(); $this->expectOutput($expected); }
/** * Parsing the score column * * @param integer $post_id The ID of the post for which to show the score. * * @return string The HTML for the SEO score indicator. */ private function parse_column_score($post_id) { if ('1' === WPSEO_Meta::get_value('meta-robots-noindex', $post_id)) { $rank = new WPSEO_Rank(WPSEO_Rank::NO_INDEX); $title = __('Post is set to noindex.', 'wordpress-seo'); WPSEO_Meta::set_value('linkdex', 0, $post_id); } elseif ('' === WPSEO_Meta::get_value('focuskw', $post_id)) { $rank = new WPSEO_Rank(WPSEO_Rank::NO_FOCUS); $title = __('Focus keyword not set.', 'wordpress-seo'); } else { $score = (int) WPSEO_Meta::get_value('linkdex', $post_id); $rank = WPSEO_Rank::from_numeric_score($score); $title = $rank->get_label(); } return $this->render_score_indicator($rank, $title); }
/** * @covers WPSEO_Twitter::site_domain */ public function test_image() { // create and go to post $post_id = $this->factory->post->create(); $this->go_to(get_permalink($post_id)); // test default image $image_url = 'http://url-default-image.jpg'; self::$class_instance->options['og_default_image'] = $image_url; $expected = $this->get_expected_image_output($image_url); self::$class_instance->image(); $this->expectOutput($expected); // reset default_image option self::$class_instance->options['og_default_image'] = ''; // TODO test og_frontpage_image // test wpseo meta value $image_url = 'http://url-singular-meta-image.jpg'; WPSEO_Meta::set_value('twitter-image', $image_url, $post_id); $expected = $this->get_expected_image_output($image_url); self::$class_instance->image(); $this->expectOutput($expected); // TODO test post thumbnail // TODO test post content image }
/** * @covers WPSEO_Frontend::author */ public function test_author() { $this->assertFalse(self::$class_instance->author()); // create and go to post $author_id = $this->factory->user->create(array('role' => 'administrator')); $post_id = $this->factory->post->create(array('post_author' => $author_id)); $this->go_to(get_permalink($post_id)); // post type default not set $this->assertFalse(self::$class_instance->author()); // post type default set but author meta not set self::$class_instance->options['noauthorship-post'] = false; $this->assertFalse(self::$class_instance->author()); // post author meta, but not set WPSEO_Meta::set_value('authorship', 'always', $post_id); $this->assertFalse(self::$class_instance->author()); // set author meta $gplus = 'https://plus.google.com/+JoostdeValk'; add_user_meta($author_id, 'googleplus', $gplus); $expected = '<link rel="author" href="' . esc_url($gplus) . '"/>' . "\n"; // post author meta and user meta set $this->assertTrue(self::$class_instance->author()); $this->expectOutput($expected); // post type default and user meta set WPSEO_Meta::set_value('authorship', '-', $post_id); $this->assertTrue(self::$class_instance->author()); $this->expectOutput($expected); }
/** * Upgrade routine to deal with the fall-out of issue #102 */ public static function re_add_durations() { global $wpdb; $query = $wpdb->prepare("SELECT post_id, meta_value\n\t\t\t\t\tFROM {$wpdb->postmeta}\n\t\t\t\t\tWHERE meta_key = %s", WPSEO_Meta::$meta_prefix . 'video_meta'); $video_metas = $wpdb->get_results($query); $query = $wpdb->prepare("SELECT post_id\n\t\t\t\t\tFROM {$wpdb->postmeta}\n\t\t\t\t\tWHERE meta_key = %s", WPSEO_Meta::$meta_prefix . 'videositemap-duration'); $known_durations = $wpdb->get_col($query); if (is_array($video_metas) && $video_metas !== array()) { foreach ($video_metas as $video) { if ($known_durations === array() || !in_array($video->post_id, $known_durations, true)) { $meta = maybe_unserialize($video->meta_value); if (isset($meta['duration'])) { WPSEO_Meta::set_value('videositemap-duration', $meta['duration'], $video->post_id); } unset($meta); } } } unset($query, $video_metas, $known_durations, $video); }
$posts = $wpdb->get_results("SELECT ID, robotsmeta FROM {$wpdb->posts}"); if (is_array($posts) && $posts !== array()) { foreach ($posts as $post) { // sync all possible settings if ($post->robotsmeta) { $pieces = explode(',', $post->robotsmeta); foreach ($pieces as $meta) { switch ($meta) { case 'noindex': WPSEO_Meta::set_value('meta-robots-noindex', '1', $post->ID); break; case 'index': WPSEO_Meta::set_value('meta-robots-noindex', '2', $post->ID); break; case 'nofollow': WPSEO_Meta::set_value('meta-robots-nofollow', '1', $post->ID); break; } } } } } unset($posts, $post, $pieces, $meta); $msg .= __(sprintf('Robots Meta values imported. We recommend %sdisabling the Robots-Meta plugin%s to avoid any conflicts.', '<a href="' . esc_url(admin_url('admin.php?page=wpseo_import&deactivate_robots_meta=1')) . '">', '</a>'), 'wordpress-seo'); } if (isset($_POST['wpseo']['importrssfooter'])) { $optold = get_option('RSSFooterOptions'); $optnew = get_option('wpseo_rss'); if ($optold['position'] == 'after') { if ($optnew['rssafter'] === '' || $optnew['rssafter'] === WPSEO_Options::get_default('wpseo_rss', 'rssafter')) { $optnew['rssafter'] = $optold['footerstring'];
/** * Parsing the score column * * @param integer $post_id The ID of the post for which to show the score. * * @return string */ private function parse_column_score($post_id) { if ('1' === WPSEO_Meta::get_value('meta-robots-noindex', $post_id)) { $rank = new WPSEO_Rank(WPSEO_Rank::NO_INDEX); $title = __('Post is set to noindex.', 'wordpress-seo'); WPSEO_Meta::set_value('linkdex', 0, $post_id); } elseif ('' === WPSEO_Meta::get_value('focuskw', $post_id)) { $rank = new WPSEO_Rank(WPSEO_Rank::NO_FOCUS); $title = __('Focus keyword not set.', 'wordpress-seo'); } else { $score = (int) WPSEO_Meta::get_value('linkdex', $post_id); $rank = WPSEO_Rank::from_numeric_score($score); $title = $rank->get_label(); } return '<div aria-hidden="true" title="' . esc_attr($title) . '" class="wpseo-score-icon ' . esc_attr($rank->get_css_class()) . '"></div><span class="screen-reader-text">' . $title . '</span>'; }
/** * Import from Joost's old robots meta plugin */ public function import_robots_meta() { global $wpdb; $posts = $wpdb->get_results("SELECT ID, robotsmeta FROM {$wpdb->posts}"); if (!$posts) { $this->set_msg(__('Error: no Robots Meta data found to import.', 'wordpress-seo')); return; } if (is_array($posts) && $posts !== array()) { foreach ($posts as $post) { // sync all possible settings if ($post->robotsmeta) { $pieces = explode(',', $post->robotsmeta); foreach ($pieces as $meta) { switch ($meta) { case 'noindex': WPSEO_Meta::set_value('meta-robots-noindex', '1', $post->ID); break; case 'index': WPSEO_Meta::set_value('meta-robots-noindex', '2', $post->ID); break; case 'nofollow': WPSEO_Meta::set_value('meta-robots-nofollow', '1', $post->ID); break; } } } } } $this->set_msg(__(sprintf('Robots Meta values imported. We recommend %sdisabling the Robots-Meta plugin%s to avoid any conflicts.', '<a href="' . esc_url(admin_url('admin.php?page=wpseo_tools&tool=import-export&deactivate_robots_meta=1#top#import-other')) . '">', '</a>'), 'wordpress-seo')); }
/** * @covers WPSEO_Frontend::canonical */ public function test_canonical() { // @todo: fix for multisite if (is_multisite()) { return; } // create and go to post $post_id = $this->factory->post->create(); $this->go_to(get_permalink($post_id)); // test default canonical $expected = get_permalink($post_id); $this->assertEquals($expected, self::$class_instance->canonical(false)); // test manual override while using no override $meta_canon = 'http://canonic.al'; WPSEO_Meta::set_value('canonical', $meta_canon, $post_id); $this->assertEquals($expected, self::$class_instance->canonical(false, false, true)); // test manual override $this->assertEquals($meta_canon, self::$class_instance->canonical(false)); // test home page $this->go_to_home(); $expected = home_url(); $this->assertEquals($expected, self::$class_instance->canonical(false, false, true)); // test search $expected = get_search_link('sample query'); $this->go_to($expected); $this->assertEquals($expected, self::$class_instance->canonical(false)); // test taxonomy pages, category pages and tag pages $category_id = wp_create_category('Category Name'); $category_link = get_category_link($category_id); $this->go_to($category_link); $expected = $category_link; $this->assertEquals($expected, self::$class_instance->canonical(false)); // @todo test post type archives // @todo test author archives // @todo test date archives // @todo test pagination // @todo test force_transport }