/wp-content/plugins/seo-ultimate/modules/opengraph/opengraph.php
PHP | 409 lines | 293 code | 82 blank | 34 comment | 37 complexity | 95593fe6b7c811c1cc92fcfc0ba35a4f MD5 | raw file
- <?php
- /**
- * Open Graph Integrator Module
- *
- * @since 7.3
- */
-
- if (class_exists('SU_Module')) {
-
- class SU_OpenGraph extends SU_Module {
-
- var $namespaces_declared = false;
- var $jlsuggest_box_post_id = false;
-
- function get_module_title() { return __('Open Graph Integrator', 'seo-ultimate'); }
- function get_menu_title() { return __('Open Graph', 'seo-ultimate'); }
-
- function get_default_settings() {
- return array(
- 'default_post_og_type' => 'article'
- , 'default_page_og_type' => 'article'
- , 'default_post_twitter_card' => 'summary'
- , 'default_page_twitter_card' => 'summary'
- , 'default_attachment_twitter_card' => 'photo'
- );
- }
-
- function init() {
- add_filter('language_attributes', array(&$this, 'html_tag_xmlns_attrs'), 1000);
- add_action('su_head', array(&$this, 'head_tag_output'));
- add_filter('su_get_setting-opengraph-twitter_site_handle', array(&$this, 'sanitize_twitter_handle'));
- add_filter('user_contactmethods', array(&$this, 'add_twitter_field'));
- }
-
- function html_tag_xmlns_attrs($attrs) {
- $this->namespaces_declared = true;
- return $attrs . ' ' . implode(' ', $this->get_xmlns_attrs());
- }
-
- function get_xmlns_attrs() {
- return array(
- 'og' => 'xmlns:og="http://ogp.me/ns#"'
- , 'fb' => 'xmlns:fb="http://ogp.me/ns/fb#"'
- );
- }
-
- function head_tag_output() {
-
- $tags = $twitter_tags = array();
-
- if (is_home()) {
-
- //Type
- $tags['og:type'] = 'blog';
-
- //Twitter Type
- $twitter_tags['twitter:card'] = 'summary';
-
- //Title
- if (!($tags['og:title'] = $this->get_setting('home_og_title')))
- $tags['og:title'] = get_bloginfo('name');
-
- //Description
- if (!($tags['og:description'] = $this->get_setting('home_og_description')))
- $tags['og:description'] = get_bloginfo('description');
-
- //URL
- $tags['og:url'] = get_bloginfo('url');
-
- //Image
- $tags['og:image'] = $this->get_setting('home_og_image');
-
- } elseif (is_singular()) {
-
- global $wp_query;
- $post = $wp_query->get_queried_object();
-
- if (is_object($post)) {
- //Type
- if (!($tags['og:type'] = $this->get_postmeta('og_type')))
- $tags['og:type'] = $this->get_setting("default_{$post->post_type}_og_type");
-
- //Twitter Type
- if (!($twitter_tags['twitter:card'] = $this->get_postmeta('twitter_card')))
- $twitter_tags['twitter:card'] = $this->get_setting("default_{$post->post_type}_twitter_card");
-
- //Title
- if (!($tags['og:title'] = $this->get_postmeta('og_title')))
- $tags['og:title'] = strip_tags( apply_filters( 'single_post_title', $post->post_title ) );
-
- //Description
- if (!($tags['og:description'] = $this->get_postmeta('og_description')))
- if ($this->plugin->call_module_func('meta-descriptions', 'get_meta_desc', $meta_desc, false) && $meta_desc)
- $tags['og:description'] = $meta_desc;
-
- //URL
- $tags['og:url'] = get_permalink($post->ID);
-
- //Image
- $tags['og:image'] = $this->jlsuggest_value_to_url($this->get_postmeta('og_image'), true);
- if (!$tags['og:image']) {
- if ('attachment' == $post->post_type)
- $tags['og:image'] = wp_get_attachment_url();
- elseif ($thumbnail_id = get_post_thumbnail_id($post->ID))
- $tags['og:image'] = wp_get_attachment_url($thumbnail_id);
- }
-
- //Additional fields
- switch ($tags['og:type']) {
- case 'article':
-
- $tags['article:published_time'] = get_the_date('Y-m-d');
- $tags['article:modified_time'] = get_the_modified_date('Y-m-d');
- $tags['article:author'] = get_author_posts_url($post->post_author);
-
- $single_category = (count(get_the_category()) == 1);
-
- $taxonomy_names = suwp::get_taxonomy_names();
- foreach ($taxonomy_names as $taxonomy_name) {
- if ($terms = get_the_terms(get_the_ID(), $taxonomy_name)) {
-
- if ($single_category && 'category' == $taxonomy_name)
- $meta_property = 'article:section';
- else
- $meta_property = 'article:tag';
-
- foreach ($terms as $term) {
- $tags[$meta_property][] = $term->name;
- }
- }
- }
-
- break;
- }
-
- //Author's Twitter Handle
- $handle = get_user_meta($post->post_author, 'twitter', true);
- $handle = $this->sanitize_twitter_handle($handle);
- $twitter_tags['twitter:creator'] = $handle;
- }
- } elseif (is_author()) {
-
- global $wp_query;
- $author = $wp_query->get_queried_object();
-
- if (is_object($author)) {
- //Type
- $tags['og:type'] = 'profile';
-
- //Title
- $tags['og:title'] = $author->display_name;
-
- //Description
- $tags['og:title'] = get_the_author_meta('description', $author->ID);
-
- //Image
- $tags['og:image'] = false;
-
- //URL
- $tags['og:url'] = get_author_posts_url($author->ID, $author->user_nicename);
-
- //First Name
- $tags['profile:first_name'] = get_the_author_meta('first_name', $author->ID);
-
- //Last Name
- $tags['profile:last_name'] = get_the_author_meta('last_name', $author->ID);
-
- //Username
- $tags['profile:username'] = $author->user_login;
-
- //Twitter Handle
- $handle = get_user_meta($author->ID, 'twitter', true);
- $handle = $this->sanitize_twitter_handle($handle);
- $twitter_tags['twitter:creator'] = $handle;
- }
- } else
- return;
-
- if ($tags['og:type'] == 'none')
- $tags['og:type'] = '';
-
- if ((!isset($tags['og:image']) || !$tags['og:image']) && $tags['og:image'] !== false)
- $tags['og:image'] = $this->jlsuggest_value_to_url($this->get_setting('default_og_image'), true);
-
- //Site Name
- if (!($tags['og:site_name'] = $this->get_setting('og_site_name')))
- $tags['og:site_name'] = get_bloginfo('name');
-
- //FB App ID
- $tags['fb:app_id'] = $this->get_setting('default_fb_app_id');
-
- //Twitter Site Handle
- $twitter_tags['twitter:site'] = $this->get_setting('twitter_site_handle');
-
- //Output meta tags
- $xmlns_attrs = $this->namespaces_declared ? array() : $this->get_xmlns_attrs();
-
- $output_formats = array(
- '<meta property="%1$s" content="%2$s" %3$s/>' => $tags
- , '<meta name="%1$s" content="%2$s" />' => $twitter_tags
- );
- foreach ($output_formats as $html_format => $format_tags) {
- foreach ($format_tags as $property => $values) {
- foreach ((array)$values as $value) {
- $property = su_esc_attr($property);
- $value = su_esc_attr($value);
- if (strlen(trim($property)) && strlen(trim($value))) {
- $xmlns = sustr::upto($property, ':');
- $xmlns_attr = empty($xmlns_attrs[$xmlns]) ? '' : $xmlns_attrs[$xmlns] . ' ';
- echo "\t";
- printf($html_format, $property, $value, $xmlns_attr);
- echo "\n";
- }
- }
- }
- }
- }
-
- function admin_page_init() {
- $this->jlsuggest_init();
- }
-
- function editor_init() {
- $this->jlsuggest_init();
- }
-
- function get_admin_page_tabs() {
-
- $postmeta_edit_tabs = $this->get_postmeta_edit_tabs(array(
- array(
- 'type' => 'dropdown'
- , 'options' => array_merge(array('' => __('Use default', 'seo-ultimate')), $this->get_type_options())
- , 'name' => 'og_type'
- , 'label' => __('Type', 'seo-ultimate')
- )
- , array(
- 'type' => 'textbox'
- , 'name' => 'og_title'
- , 'label' => __('Title', 'seo-ultimate')
- )
- , array(
- 'type' => 'textbox'
- , 'name' => 'og_description'
- , 'label' => __('Description', 'seo-ultimate')
- )
- , array(
- 'type' => 'jlsuggest'
-