示例#1
0
 public function filter_head_cache_salt($salt, $use_post = false)
 {
     switch (SucomUtil::crawler_name()) {
         case 'pinterest':
             $salt .= '_crawler:' . SucomUtil::crawler_name();
             break;
     }
     return $salt;
 }
示例#2
0
 public function __construct(&$plugin)
 {
     $this->p =& $plugin;
     $this->p->util->add_img_sizes_from_opts(array('og_img' => 'opengraph', 'rp_img' => 'opengraph-rp'));
     switch (SucomUtil::crawler_name()) {
         case 'pinterest':
             $this->size_name = $this->p->cf['lca'] . '-opengraph-rp';
             break;
         default:
             $this->size_name = $this->p->cf['lca'] . '-opengraph';
             break;
     }
     add_filter('language_attributes', array(&$this, 'add_doctype'));
 }
示例#3
0
 public function __construct(&$plugin)
 {
     $this->p =& $plugin;
     switch (SucomUtil::crawler_name()) {
         case 'pinterest':
             $this->meta_pre = 'rp';
             $this->size_name = $this->p->cf['lca'] . '-richpin';
             break;
         default:
             $this->meta_pre = 'og';
             $this->size_name = $this->p->cf['lca'] . '-opengraph';
             break;
     }
     $this->p->util->add_plugin_filters($this, array('plugin_image_sizes' => 1));
     add_filter('language_attributes', array(&$this, 'add_doctype'));
 }
示例#4
0
 public static function crawler_name($id = '')
 {
     if (self::$crawler_name === false) {
         // optimize perf - only check once
         $str = $_SERVER['HTTP_USER_AGENT'];
         switch (true) {
             // "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
             case strpos($str, 'facebookexternalhit/') === 0:
                 self::$crawler_name = 'facebook';
                 break;
                 // "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
             // "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
             case strpos($str, 'compatible; Googlebot/') !== false:
                 self::$crawler_name = 'google';
                 break;
                 // "Pinterest/0.1 +http://pinterest.com/"
             // "Pinterest/0.1 +http://pinterest.com/"
             case strpos($str, 'Pinterest/') === 0:
                 self::$crawler_name = 'pinterest';
                 break;
                 // "Twitterbot/1.0"
             // "Twitterbot/1.0"
             case strpos($str, 'Twitterbot/') === 0:
                 self::$crawler_name = 'twitter';
                 break;
                 // "W3C_Validator/1.3 http://validator.w3.org/services"
             // "W3C_Validator/1.3 http://validator.w3.org/services"
             case strpos($str, 'W3C_Validator/') === 0:
                 self::$crawler_name = 'w3c';
                 break;
         }
     }
     if (!empty($id)) {
         return $id === self::$crawler_name ? true : false;
     } else {
         return self::$crawler_name;
     }
 }
 public function __construct(&$plugin)
 {
     $this->p =& $plugin;
     $crawler_name = SucomUtil::crawler_name();
     if ($this->p->debug->enabled) {
         $this->p->debug->mark();
     }
     add_filter('amp_post_template_metadata', array(&$this, 'filter_amp_post_template_metadata'), 9000, 2);
     // pinterest does not (currently) read json markup
     switch ($crawler_name) {
         case 'pinterest':
             break;
         default:
             $this->p->util->add_plugin_filters($this, array('add_schema_head_attributes' => '__return_false', 'add_schema_meta_array' => '__return_false', 'add_schema_noscript_array' => '__return_false', 'json_data_https_schema_org_thing' => 5), -100);
             // make sure we run first
             break;
     }
     $this->p->util->add_plugin_filters($this, array('get_md_defaults' => 2));
     if (is_admin()) {
         $this->p->util->add_plugin_actions($this, array('admin_post_head' => 1));
         $this->p->util->add_plugin_filters($this, array('option_type' => 2, 'save_post_options' => 4, 'post_cache_transients' => 4, 'pub_google_rows' => 2));
         $this->p->util->add_plugin_filters($this, array('messages_tooltip_meta' => 2, 'status_gpl_features' => 3, 'status_pro_features' => 3), 10, 'wpssojson');
     }
 }
 public function get_array(&$og = array(), $use_post = false, $obj = false)
 {
     if (!is_object($obj)) {
         $obj = $this->p->util->get_post_object($use_post);
     }
     $post_id = empty($obj->ID) || empty($obj->post_type) || !is_singular() && $use_post === false ? 0 : $obj->ID;
     $post_type = '';
     $video_previews = 0;
     $og_max = $this->p->util->get_max_nums($post_id, 'post');
     // post_id 0 returns the plugin settings
     $og = apply_filters($this->p->cf['lca'] . '_og_seed', $og, $use_post, $obj);
     if (!empty($og) && $this->p->debug->enabled) {
         $this->p->debug->log($this->p->cf['lca'] . '_og_seed filter returned:');
         $this->p->debug->log($og);
     }
     if (!isset($og['fb:admins']) && !empty($this->p->options['fb_admins'])) {
         foreach (explode(',', $this->p->options['fb_admins']) as $fb_admin) {
             $og['fb:admins'][] = trim($fb_admin);
         }
     }
     if (!isset($og['fb:app_id'])) {
         $og['fb:app_id'] = $this->p->options['fb_app_id'];
     }
     if (!isset($og['og:url'])) {
         $og['og:url'] = $this->p->util->get_sharing_url($use_post, true, $this->p->util->get_source_id('opengraph'));
     }
     // define the type after the url
     if (!isset($og['og:type'])) {
         // singular posts / pages are articles by default
         // check the post_type for a match with a known open graph type
         if (is_singular() || $use_post !== false) {
             if (!empty($obj->post_type)) {
                 $post_type = $obj->post_type;
             }
             if (isset($this->p->cf['head']['og_type_ns'][$post_type])) {
                 $og['og:type'] = $post_type;
             } else {
                 $og['og:type'] = 'article';
             }
             // check for default author info on indexes and searches
         } elseif ($this->p->util->force_default_author($use_post, 'og') && !empty($this->p->options['og_def_author_id'])) {
             $og['og:type'] = 'article';
             if (!isset($og['article:author'])) {
                 $og['article:author'] = $this->p->mods['util']['user']->get_article_author($this->p->options['og_def_author_id']);
             }
             // default for everything else is 'website'
         } else {
             $og['og:type'] = 'website';
         }
         $og['og:type'] = apply_filters($this->p->cf['lca'] . '_og_type', $og['og:type'], $use_post);
     }
     if (!isset($og['og:locale'])) {
         // get the current or configured language for og:locale
         $lang = empty($this->p->options['fb_lang']) ? SucomUtil::get_locale($post_id) : $this->p->options['fb_lang'];
         $lang = apply_filters($this->p->cf['lca'] . '_lang', $lang, SucomUtil::get_pub_lang('facebook'), $post_id);
         $og['og:locale'] = $lang;
     }
     if (!isset($og['og:site_name'])) {
         $og['og:site_name'] = $this->get_site_name($post_id);
     }
     if (!isset($og['og:title'])) {
         $og['og:title'] = $this->p->webpage->get_title($this->p->options['og_title_len'], '...', $use_post);
     }
     if (!isset($og['og:description'])) {
         $og['og:description'] = $this->p->webpage->get_description($this->p->options['og_desc_len'], '...', $use_post);
     }
     // if the page is an article, then define the other article meta tags
     if (isset($og['og:type']) && $og['og:type'] == 'article') {
         if (!isset($og['article:author'])) {
             if (is_singular() || $use_post !== false) {
                 if (!empty($obj->post_author)) {
                     $og['article:author'] = $this->p->mods['util']['user']->get_article_author($obj->post_author);
                 } elseif (!empty($this->p->options['og_def_author_id'])) {
                     $og['article:author'] = $this->p->mods['util']['user']->get_article_author($this->p->options['og_def_author_id']);
                 }
             }
         }
         if (!isset($og['article:publisher'])) {
             $og['article:publisher'] = $this->p->options['fb_publisher_url'];
         }
         if (!isset($og['article:tag'])) {
             $og['article:tag'] = $this->p->webpage->get_tags($post_id);
         }
         if (!isset($og['article:section'])) {
             $og['article:section'] = $this->p->webpage->get_section($post_id);
         }
         if (!isset($og['article:published_time'])) {
             $og['article:published_time'] = trim(get_the_date('c'));
         }
         if (!isset($og['article:modified_time'])) {
             $og['article:modified_time'] = trim(get_the_modified_date('c'));
         }
     }
     // get all videos
     // call before getting all images to find / use preview images
     if (!isset($og['og:video'])) {
         if (empty($og_max['og_vid_max'])) {
             if ($this->p->debug->enabled) {
                 $this->p->debug->log('videos disabled: maximum videos = 0');
             }
         } else {
             $og['og:video'] = $this->get_all_videos($og_max['og_vid_max'], $post_id, true, 'og');
             if (is_array($og['og:video'])) {
                 foreach ($og['og:video'] as $val) {
                     if (!empty($val['og:image'])) {
                         $video_previews++;
                     }
                 }
                 if ($video_previews > 0) {
                     $og_max['og_img_max'] -= $video_previews;
                     if ($this->p->debug->enabled) {
                         $this->p->debug->log($video_previews . ' video preview images found (og_img_max adjusted to ' . $og_max['og_img_max'] . ')');
                     }
                 }
             }
         }
     }
     // get all images
     if (!isset($og['og:image'])) {
         if (empty($og_max['og_img_max'])) {
             if ($this->p->debug->enabled) {
                 $this->p->debug->log('images disabled: maximum images = 0');
             }
         } else {
             if (is_admin()) {
                 $img_sizes = array('rp' => $this->p->cf['lca'] . '-richpin', 'og' => $this->p->cf['lca'] . '-opengraph');
             } else {
                 switch (SucomUtil::crawler_name()) {
                     case 'pinterest':
                         $img_sizes = array('rp' => $this->p->cf['lca'] . '-richpin');
                         break;
                     default:
                         $img_sizes = array('og' => $this->p->cf['lca'] . '-opengraph');
                         break;
                 }
             }
             foreach ($img_sizes as $meta_pre => $size_name) {
                 // only check for dupes on last image size
                 $check_dupes = is_admin() && $meta_pre !== 'og' ? false : true;
                 $og['og:image'] = $this->get_all_images($og_max['og_img_max'], $size_name, $post_id, $check_dupes, $meta_pre);
                 // if there's no image, and no video preview image, then add the default image for non-index webpages
                 if (empty($og['og:image']) && $video_previews === 0 && (is_singular() || $use_post !== false)) {
                     $og['og:image'] = $this->p->media->get_default_image($og_max['og_img_max'], $size_name, $check_dupes);
                 }
             }
         }
     }
     // only a few opengraph meta tags are allowed to be empty
     foreach ($og as $key => $val) {
         switch ($key) {
             case 'og:locale':
             case 'og:site_name':
             case 'og:description':
                 break;
             default:
                 if ($val === '' || is_array($val) && empty($val)) {
                     unset($og[$key]);
                 }
                 break;
         }
     }
     // twitter cards are hooked into this filter to use existing open graph values
     return apply_filters($this->p->cf['lca'] . '_og', $og, $use_post, $obj);
 }
示例#7
0
 public function get_array($use_post = false, $obj = false, &$og = array())
 {
     if ($this->p->debug->enabled) {
         $this->p->debug->mark();
     }
     if (!is_object($obj)) {
         $obj = $this->p->util->get_post_object($use_post);
     }
     $post_id = empty($obj->ID) || empty($obj->post_type) || !SucomUtil::is_post_page($use_post) ? 0 : $obj->ID;
     // counter for video previews found
     $video_previews = 0;
     // post_id 0 returns the default plugin settings
     $og_max = $this->p->util->get_max_nums($post_id, 'post');
     $og = apply_filters($this->p->cf['lca'] . '_og_seed', $og, $use_post, $obj);
     if (!empty($og) && $this->p->debug->enabled) {
         $this->p->debug->log($this->p->cf['lca'] . '_og_seed filter returned:');
         $this->p->debug->log($og);
     }
     if (!isset($og['fb:admins']) && !empty($this->p->options['fb_admins'])) {
         foreach (explode(',', $this->p->options['fb_admins']) as $fb_admin) {
             $og['fb:admins'][] = trim($fb_admin);
         }
     }
     if (!isset($og['fb:app_id'])) {
         $og['fb:app_id'] = $this->p->options['fb_app_id'];
     }
     if (!isset($og['og:url'])) {
         $og['og:url'] = $this->p->util->get_sharing_url($use_post, true, $this->p->util->get_source_id('opengraph'));
     }
     // define the type after the url
     if (!isset($og['og:type'])) {
         // singular posts / pages are articles by default
         // check the post_type for a match with a known open graph type
         if (SucomUtil::is_post_page($use_post)) {
             if (!empty($obj->post_type) && isset($this->p->cf['head']['og_type_ns'][$obj->post_type])) {
                 $og['og:type'] = $obj->post_type;
             } else {
                 $og['og:type'] = 'article';
             }
             // check for default author info on indexes and searches
         } elseif ($this->p->util->force_default_author($use_post, 'og') && !empty($this->p->options['og_def_author_id'])) {
             $og['og:type'] = 'article';
             // meta tag not defined or value is null
             if (!isset($og['article:author'])) {
                 $og['article:author'] = $this->p->mods['util']['user']->get_article_author($this->p->options['og_def_author_id']);
             }
             // default for everything else is 'website'
         } else {
             $og['og:type'] = 'website';
         }
         $og['og:type'] = apply_filters($this->p->cf['lca'] . '_og_type', $og['og:type'], $use_post);
         // pre-define basic open graph meta tags for this type
         if (isset($this->p->cf['head']['og_type_mt'][$og['og:type']])) {
             foreach ($this->p->cf['head']['og_type_mt'][$og['og:type']] as $mt_name) {
                 if (!isset($og[$mt_name])) {
                     $og[$mt_name] = null;
                     if ($this->p->debug->enabled) {
                         $this->p->debug->log($og['og:type'] . ' pre-defined mt: ' . $mt_name);
                     }
                 }
             }
         }
     }
     if (!isset($og['og:locale'])) {
         // get the current or configured language for og:locale
         $lang = empty($this->p->options['fb_lang']) ? SucomUtil::get_locale($post_id) : $this->p->options['fb_lang'];
         $lang = apply_filters($this->p->cf['lca'] . '_lang', $lang, SucomUtil::get_pub_lang('facebook'), $post_id);
         $og['og:locale'] = $lang;
     }
     if (!isset($og['og:site_name'])) {
         $og['og:site_name'] = $this->get_site_name($post_id);
     }
     if (!isset($og['og:title'])) {
         $og['og:title'] = $this->p->webpage->get_title($this->p->options['og_title_len'], '...', $use_post);
     }
     if (!isset($og['og:description'])) {
         $og['og:description'] = $this->p->webpage->get_description($this->p->options['og_desc_len'], '...', $use_post);
     }
     // if the page is an article, then define the other article meta tags
     if (isset($og['og:type']) && $og['og:type'] == 'article') {
         // meta tag not defined or value is null
         if (!isset($og['article:author'])) {
             if (SucomUtil::is_post_page($use_post)) {
                 if (!empty($obj->post_author)) {
                     $og['article:author'] = $this->p->mods['util']['user']->get_article_author($obj->post_author);
                 } elseif (!empty($this->p->options['og_def_author_id'])) {
                     $og['article:author'] = $this->p->mods['util']['user']->get_article_author($this->p->options['og_def_author_id']);
                 }
             }
         }
         // meta tag not defined or value is null
         if (!isset($og['article:publisher'])) {
             $og['article:publisher'] = $this->p->options['fb_publisher_url'];
         }
         // meta tag not defined or value is null
         if (!isset($og['article:tag'])) {
             $og['article:tag'] = $this->p->webpage->get_tags($post_id);
         }
         // meta tag not defined or value is null
         if (!isset($og['article:section'])) {
             $og['article:section'] = $this->p->webpage->get_section($post_id);
         }
         // meta tag not defined or value is null
         if (!isset($og['article:published_time'])) {
             $og['article:published_time'] = trim(get_the_date('c'));
         }
         // meta tag not defined or value is null
         if (!isset($og['article:modified_time'])) {
             $og['article:modified_time'] = trim(get_the_modified_date('c'));
         }
     }
     // get all videos
     // call before getting all images to find / use preview images
     if (!isset($og['og:video'])) {
         if (empty($og_max['og_vid_max'])) {
             if ($this->p->debug->enabled) {
                 $this->p->debug->log('videos disabled: maximum videos = 0');
             }
         } else {
             $og['og:video'] = $this->get_all_videos($og_max['og_vid_max'], $post_id, 'post', false, 'og');
             if (!empty($og['og:video']) && is_array($og['og:video'])) {
                 foreach ($og['og:video'] as $val) {
                     if (!empty($val['og:image'])) {
                         $video_previews++;
                     }
                 }
                 if ($video_previews > 0) {
                     $og_max['og_img_max'] -= $video_previews;
                     if ($this->p->debug->enabled) {
                         $this->p->debug->log($video_previews . ' video preview images found (og_img_max adjusted to ' . $og_max['og_img_max'] . ')');
                     }
                 }
             }
         }
     }
     // get all images
     if (!isset($og['og:image'])) {
         if (empty($og_max['og_img_max'])) {
             if ($this->p->debug->enabled) {
                 $this->p->debug->log('images disabled: maximum images = 0');
             }
         } else {
             $img_sizes = array();
             $crawler_name = SucomUtil::crawler_name();
             if ((is_admin() || $crawler_name === 'pinterest') && !SucomUtil::get_const('WPSSO_RICH_PIN_DISABLE')) {
                 $img_sizes['rp'] = $this->p->cf['lca'] . '-richpin';
             }
             $img_sizes['og'] = $this->p->cf['lca'] . '-opengraph';
             // must be last for meta tags preview
             foreach ($img_sizes as $md_pre => $size_name) {
                 // only check for dupes on last image size
                 $check_dupes = is_admin() && $md_pre !== 'og' ? false : true;
                 $og['og:image'] = $this->get_all_images($og_max['og_img_max'], $size_name, $post_id, $check_dupes, $md_pre);
                 // if there's no image, and no video preview image, then add the default image for non-index webpages
                 if (empty($og['og:image']) && $video_previews === 0 && SucomUtil::is_post_page($use_post)) {
                     $og['og:image'] = $this->p->media->get_default_image($og_max['og_img_max'], $size_name, $check_dupes);
                 }
             }
         }
     }
     return apply_filters($this->p->cf['lca'] . '_og', $og, $use_post, $obj);
 }
示例#8
0
 public function get_article_author($author_id, $url_field = 'og_author_field')
 {
     $ret = array();
     if (!empty($author_id)) {
         $ret[] = $this->get_author_website_url($author_id, $this->p->options[$url_field]);
         // add the author's name if this is the Pinterest crawler
         if (SucomUtil::crawler_name('pinterest') === true) {
             $ret[] = $this->get_author_name($author_id, $this->p->options['rp_author_name']);
         }
     } elseif ($this->p->debug->enabled) {
         $this->p->debug->log('author_id provided is empty');
     }
     return $ret;
 }
示例#9
0
 public function get_author_profile_url($author_ids, $url_field = 'og_author_field')
 {
     $ret = array();
     if (!empty($author_ids)) {
         if (!is_array($author_ids)) {
             $author_ids = array($author_ids);
         }
         foreach ($author_ids as $author_id) {
             if (!empty($author_id)) {
                 if (SucomUtil::crawler_name('pinterest') === true) {
                     $ret[] = $this->get_author_name($author_id, $this->p->options['rp_author_name']);
                 } else {
                     $ret[] = $this->get_author_website_url($author_id, $this->p->options[$url_field]);
                 }
             }
         }
     }
     return $ret;
 }