PageRenderTime 194ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/seo-ultimate/modules/opengraph/opengraph.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 409 lines | 293 code | 82 blank | 34 comment | 37 complexity | 95593fe6b7c811c1cc92fcfc0ba35a4f MD5 | raw file
  1. <?php
  2. /**
  3. * Open Graph Integrator Module
  4. *
  5. * @since 7.3
  6. */
  7. if (class_exists('SU_Module')) {
  8. class SU_OpenGraph extends SU_Module {
  9. var $namespaces_declared = false;
  10. var $jlsuggest_box_post_id = false;
  11. function get_module_title() { return __('Open Graph Integrator', 'seo-ultimate'); }
  12. function get_menu_title() { return __('Open Graph', 'seo-ultimate'); }
  13. function get_default_settings() {
  14. return array(
  15. 'default_post_og_type' => 'article'
  16. , 'default_page_og_type' => 'article'
  17. , 'default_post_twitter_card' => 'summary'
  18. , 'default_page_twitter_card' => 'summary'
  19. , 'default_attachment_twitter_card' => 'photo'
  20. );
  21. }
  22. function init() {
  23. add_filter('language_attributes', array(&$this, 'html_tag_xmlns_attrs'), 1000);
  24. add_action('su_head', array(&$this, 'head_tag_output'));
  25. add_filter('su_get_setting-opengraph-twitter_site_handle', array(&$this, 'sanitize_twitter_handle'));
  26. add_filter('user_contactmethods', array(&$this, 'add_twitter_field'));
  27. }
  28. function html_tag_xmlns_attrs($attrs) {
  29. $this->namespaces_declared = true;
  30. return $attrs . ' ' . implode(' ', $this->get_xmlns_attrs());
  31. }
  32. function get_xmlns_attrs() {
  33. return array(
  34. 'og' => 'xmlns:og="http://ogp.me/ns#"'
  35. , 'fb' => 'xmlns:fb="http://ogp.me/ns/fb#"'
  36. );
  37. }
  38. function head_tag_output() {
  39. $tags = $twitter_tags = array();
  40. if (is_home()) {
  41. //Type
  42. $tags['og:type'] = 'blog';
  43. //Twitter Type
  44. $twitter_tags['twitter:card'] = 'summary';
  45. //Title
  46. if (!($tags['og:title'] = $this->get_setting('home_og_title')))
  47. $tags['og:title'] = get_bloginfo('name');
  48. //Description
  49. if (!($tags['og:description'] = $this->get_setting('home_og_description')))
  50. $tags['og:description'] = get_bloginfo('description');
  51. //URL
  52. $tags['og:url'] = get_bloginfo('url');
  53. //Image
  54. $tags['og:image'] = $this->get_setting('home_og_image');
  55. } elseif (is_singular()) {
  56. global $wp_query;
  57. $post = $wp_query->get_queried_object();
  58. if (is_object($post)) {
  59. //Type
  60. if (!($tags['og:type'] = $this->get_postmeta('og_type')))
  61. $tags['og:type'] = $this->get_setting("default_{$post->post_type}_og_type");
  62. //Twitter Type
  63. if (!($twitter_tags['twitter:card'] = $this->get_postmeta('twitter_card')))
  64. $twitter_tags['twitter:card'] = $this->get_setting("default_{$post->post_type}_twitter_card");
  65. //Title
  66. if (!($tags['og:title'] = $this->get_postmeta('og_title')))
  67. $tags['og:title'] = strip_tags( apply_filters( 'single_post_title', $post->post_title ) );
  68. //Description
  69. if (!($tags['og:description'] = $this->get_postmeta('og_description')))
  70. if ($this->plugin->call_module_func('meta-descriptions', 'get_meta_desc', $meta_desc, false) && $meta_desc)
  71. $tags['og:description'] = $meta_desc;
  72. //URL
  73. $tags['og:url'] = get_permalink($post->ID);
  74. //Image
  75. $tags['og:image'] = $this->jlsuggest_value_to_url($this->get_postmeta('og_image'), true);
  76. if (!$tags['og:image']) {
  77. if ('attachment' == $post->post_type)
  78. $tags['og:image'] = wp_get_attachment_url();
  79. elseif ($thumbnail_id = get_post_thumbnail_id($post->ID))
  80. $tags['og:image'] = wp_get_attachment_url($thumbnail_id);
  81. }
  82. //Additional fields
  83. switch ($tags['og:type']) {
  84. case 'article':
  85. $tags['article:published_time'] = get_the_date('Y-m-d');
  86. $tags['article:modified_time'] = get_the_modified_date('Y-m-d');
  87. $tags['article:author'] = get_author_posts_url($post->post_author);
  88. $single_category = (count(get_the_category()) == 1);
  89. $taxonomy_names = suwp::get_taxonomy_names();
  90. foreach ($taxonomy_names as $taxonomy_name) {
  91. if ($terms = get_the_terms(get_the_ID(), $taxonomy_name)) {
  92. if ($single_category && 'category' == $taxonomy_name)
  93. $meta_property = 'article:section';
  94. else
  95. $meta_property = 'article:tag';
  96. foreach ($terms as $term) {
  97. $tags[$meta_property][] = $term->name;
  98. }
  99. }
  100. }
  101. break;
  102. }
  103. //Author's Twitter Handle
  104. $handle = get_user_meta($post->post_author, 'twitter', true);
  105. $handle = $this->sanitize_twitter_handle($handle);
  106. $twitter_tags['twitter:creator'] = $handle;
  107. }
  108. } elseif (is_author()) {
  109. global $wp_query;
  110. $author = $wp_query->get_queried_object();
  111. if (is_object($author)) {
  112. //Type
  113. $tags['og:type'] = 'profile';
  114. //Title
  115. $tags['og:title'] = $author->display_name;
  116. //Description
  117. $tags['og:title'] = get_the_author_meta('description', $author->ID);
  118. //Image
  119. $tags['og:image'] = false;
  120. //URL
  121. $tags['og:url'] = get_author_posts_url($author->ID, $author->user_nicename);
  122. //First Name
  123. $tags['profile:first_name'] = get_the_author_meta('first_name', $author->ID);
  124. //Last Name
  125. $tags['profile:last_name'] = get_the_author_meta('last_name', $author->ID);
  126. //Username
  127. $tags['profile:username'] = $author->user_login;
  128. //Twitter Handle
  129. $handle = get_user_meta($author->ID, 'twitter', true);
  130. $handle = $this->sanitize_twitter_handle($handle);
  131. $twitter_tags['twitter:creator'] = $handle;
  132. }
  133. } else
  134. return;
  135. if ($tags['og:type'] == 'none')
  136. $tags['og:type'] = '';
  137. if ((!isset($tags['og:image']) || !$tags['og:image']) && $tags['og:image'] !== false)
  138. $tags['og:image'] = $this->jlsuggest_value_to_url($this->get_setting('default_og_image'), true);
  139. //Site Name
  140. if (!($tags['og:site_name'] = $this->get_setting('og_site_name')))
  141. $tags['og:site_name'] = get_bloginfo('name');
  142. //FB App ID
  143. $tags['fb:app_id'] = $this->get_setting('default_fb_app_id');
  144. //Twitter Site Handle
  145. $twitter_tags['twitter:site'] = $this->get_setting('twitter_site_handle');
  146. //Output meta tags
  147. $xmlns_attrs = $this->namespaces_declared ? array() : $this->get_xmlns_attrs();
  148. $output_formats = array(
  149. '<meta property="%1$s" content="%2$s" %3$s/>' => $tags
  150. , '<meta name="%1$s" content="%2$s" />' => $twitter_tags
  151. );
  152. foreach ($output_formats as $html_format => $format_tags) {
  153. foreach ($format_tags as $property => $values) {
  154. foreach ((array)$values as $value) {
  155. $property = su_esc_attr($property);
  156. $value = su_esc_attr($value);
  157. if (strlen(trim($property)) && strlen(trim($value))) {
  158. $xmlns = sustr::upto($property, ':');
  159. $xmlns_attr = empty($xmlns_attrs[$xmlns]) ? '' : $xmlns_attrs[$xmlns] . ' ';
  160. echo "\t";
  161. printf($html_format, $property, $value, $xmlns_attr);
  162. echo "\n";
  163. }
  164. }
  165. }
  166. }
  167. }
  168. function admin_page_init() {
  169. $this->jlsuggest_init();
  170. }
  171. function editor_init() {
  172. $this->jlsuggest_init();
  173. }
  174. function get_admin_page_tabs() {
  175. $postmeta_edit_tabs = $this->get_postmeta_edit_tabs(array(
  176. array(
  177. 'type' => 'dropdown'
  178. , 'options' => array_merge(array('' => __('Use default', 'seo-ultimate')), $this->get_type_options())
  179. , 'name' => 'og_type'
  180. , 'label' => __('Type', 'seo-ultimate')
  181. )
  182. , array(
  183. 'type' => 'textbox'
  184. , 'name' => 'og_title'
  185. , 'label' => __('Title', 'seo-ultimate')
  186. )
  187. , array(
  188. 'type' => 'textbox'
  189. , 'name' => 'og_description'
  190. , 'label' => __('Description', 'seo-ultimate')
  191. )
  192. , array(
  193. 'type' => 'jlsuggest'
  194. , 'name' => 'og_image'
  195. , 'label' => __('Image', 'seo-ultimate')
  196. , 'options' => array(
  197. 'params' => 'types=posttype_attachment&post_mime_type=image/*'
  198. ))
  199. ));
  200. //Remove the Image boxes from the Media tab
  201. //(it's obvious what the og:image of an attachment should be...)
  202. unset($postmeta_edit_tabs['attachment']['callback'][5][3]);
  203. return array_merge(
  204. array(
  205. array('title' => __('Sitewide Values', 'seo-ultimate'), 'id' => 'su-sitewide-values', 'callback' => 'global_tab')
  206. , array('title' => __('Default Values', 'seo-ultimate'), 'id' => 'su-default-values', 'callback' => 'defaults_tab')
  207. , array('title' => __('Blog Homepage', 'seo-ultimate'), 'id' => 'su-homepage', 'callback' => 'home_tab')
  208. )
  209. , $postmeta_edit_tabs
  210. );
  211. }
  212. function global_tab() {
  213. $this->admin_form_table_start();
  214. $this->textbox('og_site_name', __('Site Name', 'seo-ultimate'), false, false, array(), array('placeholder' => get_bloginfo('name')));
  215. $this->textbox('default_fb_app_id', __('Facebook App ID', 'seo-ultimate'));
  216. $this->textbox('twitter_site_handle', __('This Site&#8217;s Twitter Handle', 'seo-ultimate'));
  217. $this->admin_form_table_end();
  218. }
  219. function defaults_tab() {
  220. $posttypes = suwp::get_post_type_objects();
  221. $this->admin_subheader(__('Default Types', 'seo-ultimate'));
  222. $this->admin_wftable_start(array(
  223. 'posttype' => __('Post Type', 'seo-ultimate')
  224. , 'og' => __('Open Graph Type', 'seo-ultimate')
  225. , 'twitter' => __('Twitter Type', 'seo-ultimate')
  226. ));
  227. foreach ($posttypes as $posttype) {
  228. echo "<tr valign='middle'>\n";
  229. echo "\t<th class='su-opengraph-posttype' scope='row'>" . esc_html($posttype->labels->name) . "</th>\n";
  230. echo "\t<td class='su-opengraph-og'>";
  231. $this->dropdown("default_{$posttype->name}_og_type", $this->get_type_options(), false, '%s', array('in_table' => false));
  232. echo "</td>\n";
  233. echo "\t<td class='su-opengraph-twitter'>";
  234. $this->dropdown("default_{$posttype->name}_twitter_card", $this->get_twitter_type_options(), false, '%s', array('in_table' => false));
  235. echo "</td>\n";
  236. echo "</tr>\n";
  237. }
  238. $this->admin_wftable_end();
  239. $this->admin_subheader(__('Default Image', 'seo-ultimate'));
  240. $this->admin_form_table_start();
  241. $this->textblock(__('In the box below, you can specify an image URL or an image from your media library to use as a default image in the event that there is no image otherwise specified for a given webpage on your site.', 'seo-ultimate'));
  242. $this->jlsuggest_box('default_og_image', __('Default Image', 'seo-ultimate'), 'types=posttype_attachment&post_mime_type=image/*');
  243. $this->admin_form_table_end();
  244. }
  245. function home_tab() {
  246. $this->admin_form_table_start();
  247. $this->textbox('home_og_title', __('Blog Homepage Title', 'seo-ultimate'), false, false, array(), array('placeholder' => get_bloginfo('name')));
  248. $this->textbox('home_og_description', __('Blog Homepage Description', 'seo-ultimate'), false, false, array(), array('placeholder' => get_bloginfo('description')));
  249. $this->jlsuggest_box('home_og_image', __('Blog Homepage Image', 'seo-ultimate'), 'types=posttype_attachment&post_mime_type=image/*');
  250. $this->admin_form_table_end();
  251. }
  252. function postmeta_fields($fields) {
  253. $fields['opengraph'][10]['og_title'] = $this->get_postmeta_textbox('og_title', __('Title:', 'seo-ultimate'));
  254. $fields['opengraph'][20]['og_description'] = $this->get_postmeta_textarea('og_description', __('Description:', 'seo-ultimate'));
  255. $fields['opengraph'][30]['og_image'] = $this->get_postmeta_jlsuggest_box('og_image', __('Image:', 'seo-ultimate'), 'types=posttype_attachment&post_mime_type=image/*');
  256. $fields['opengraph'][40]['og_type'] = $this->get_postmeta_dropdown('og_type', array_merge(array('' => __('Use default', 'seo-ultimate')), $this->get_type_options()), __('Open Graph Type:', 'seo-ultimate'));
  257. $fields['opengraph'][50]['twitter_card'] = $this->get_postmeta_dropdown('twitter_card', array_merge(array('' => __('Use default', 'seo-ultimate')), $this->get_twitter_type_options()), __('Twitter Type:', 'seo-ultimate'));
  258. return $fields;
  259. }
  260. function get_postmeta_jlsuggest_boxes($jls_boxes) {
  261. $this->jlsuggest_box_post_id = suwp::get_post_id();
  262. return parent::get_postmeta_jlsuggest_boxes($jls_boxes);
  263. }
  264. function get_input_element($type, $name, $value=null, $extra=false, $inputid=true) {
  265. $name_parts = explode('_', $name);
  266. if (isset($name_parts[1]) && is_numeric($post_id = $name_parts[1]))
  267. $this->jlsuggest_box_post_id = $post_id;
  268. else
  269. $this->jlsuggest_box_post_id = false;
  270. return parent::get_input_element($type, $name, $value, $extra, $inputid);
  271. }
  272. function get_jlsuggest_box($name, $value, $params='', $placeholder='') {
  273. if (empty($value) && $this->jlsuggest_box_post_id && $thumbnail_id = get_post_thumbnail_id($this->jlsuggest_box_post_id)) {
  274. $selected_post = get_post($thumbnail_id);
  275. $placeholder = sprintf(__('Featured Image: %s', 'seo-ultimate'), $selected_post->post_title);
  276. }
  277. return parent::get_jlsuggest_box($name, $value, $params, $placeholder);
  278. }
  279. function get_type_options() {
  280. return array(
  281. 'none' => __('None', 'seo-ultimate')
  282. , __('Internet', 'seo-ultimate') => array(
  283. 'article' => __('Article', 'seo-ultimate')
  284. , 'blog' => __('Blog', 'seo-ultimate')
  285. , 'profile' => __('Profile', 'seo-ultimate')
  286. , 'website' => __('Website', 'seo-ultimate')
  287. ),__('Products', 'seo-ultimate') => array(
  288. 'book' => __('Book', 'seo-ultimate')
  289. ),__('Music', 'seo-ultimate') => array(
  290. 'music.album' => __('Album', 'seo-ultimate')
  291. , 'music.playlist' => __('Playlist', 'seo-ultimate')
  292. , 'music.radio_station' => __('Radio Station', 'seo-ultimate')
  293. , 'music.song' => __('Song', 'seo-ultimate')
  294. ),__('Videos', 'seo-ultimate') => array(
  295. 'video.movie' => __('Movie', 'seo-ultimate')
  296. , 'video.episode' => __('TV Episode', 'seo-ultimate')
  297. , 'video.tv_show' => __('TV Show', 'seo-ultimate')
  298. , 'video.other' => __('Video', 'seo-ultimate')
  299. )
  300. );
  301. }
  302. function get_twitter_type_options() {
  303. return array(
  304. 'summary' => __('Regular', 'seo-ultimate')
  305. , 'photo' => __('Photo', 'seo-ultimate')
  306. );
  307. }
  308. function sanitize_twitter_handle($value) {
  309. if (strpos($value, '/') === false) {
  310. $handle = ltrim($value, '@');
  311. } else {
  312. $url_parts = explode('/', $value);
  313. $handle = array_pop($url_parts);
  314. }
  315. $handle = sustr::preg_filter('a-zA-Z0-9_', $handle);
  316. $handle = trim($handle);
  317. if ($handle)
  318. $handle = "@$handle";
  319. return $handle;
  320. }
  321. function add_twitter_field( $contactmethods ) {
  322. $contactmethods['twitter'] = __('Twitter Handle', 'seo-ultimate');
  323. return $contactmethods;
  324. }
  325. }
  326. }
  327. ?>