WPSEO_OpenGraph

This code adds the OpenGraph output.

Defined (1)

The class is defined in the following location(s).

/frontend/class-opengraph.php  
  1. class WPSEO_OpenGraph { 
  2.  
  3. /** 
  4. * @var array $options Options for the OpenGraph Settings 
  5. */ 
  6. public $options = array(); 
  7.  
  8. /** 
  9. * Class constructor. 
  10. */ 
  11. public function __construct() { 
  12. $this->options = WPSEO_Options::get_option( 'wpseo_social' ); 
  13.  
  14. if ( isset( $GLOBALS['fb_ver'] ) || class_exists( 'Facebook_Loader', false ) ) { 
  15. add_filter( 'fb_meta_tags', array( $this, 'facebook_filter' ), 10, 1 ); 
  16. else { 
  17. add_filter( 'language_attributes', array( $this, 'add_opengraph_namespace' ), 15 ); 
  18.  
  19. add_action( 'wpseo_opengraph', array( $this, 'locale' ), 1 ); 
  20. add_action( 'wpseo_opengraph', array( $this, 'type' ), 5 ); 
  21. add_action( 'wpseo_opengraph', array( $this, 'og_title' ), 10 ); 
  22. add_action( 'wpseo_opengraph', array( $this, 'site_owner' ), 20 ); 
  23. add_action( 'wpseo_opengraph', array( $this, 'description' ), 11 ); 
  24. add_action( 'wpseo_opengraph', array( $this, 'url' ), 12 ); 
  25. add_action( 'wpseo_opengraph', array( $this, 'site_name' ), 13 ); 
  26. add_action( 'wpseo_opengraph', array( $this, 'website_facebook' ), 14 ); 
  27. if ( is_singular() && ! is_front_page() ) { 
  28. add_action( 'wpseo_opengraph', array( $this, 'article_author_facebook' ), 15 ); 
  29. add_action( 'wpseo_opengraph', array( $this, 'tags' ), 16 ); 
  30. add_action( 'wpseo_opengraph', array( $this, 'category' ), 17 ); 
  31. add_action( 'wpseo_opengraph', array( $this, 'publish_date' ), 19 ); 
  32.  
  33. add_action( 'wpseo_opengraph', array( $this, 'image' ), 30 ); 
  34. add_filter( 'jetpack_enable_open_graph', '__return_false' ); 
  35. add_action( 'wpseo_head', array( $this, 'opengraph' ), 30 ); 
  36.  
  37. /** 
  38. * Main OpenGraph output. 
  39. */ 
  40. public function opengraph() { 
  41. wp_reset_query(); 
  42. /** 
  43. * Action: 'wpseo_opengraph' - Hook to add all Facebook OpenGraph output to so they're close together. 
  44. */ 
  45. do_action( 'wpseo_opengraph' ); 
  46.  
  47. /** 
  48. * Internal function to output FB tags. This also adds an output filter to each bit of output based on the property. 
  49. * @param string $property Property attribute value. 
  50. * @param string $content Content attribute value. 
  51. * @return boolean 
  52. */ 
  53. public function og_tag( $property, $content ) { 
  54. $og_property = str_replace( ':', '_', $property ); 
  55. /** 
  56. * Filter: 'wpseo_og_' . $og_property - Allow developers to change the content of specific OG meta tags. 
  57. * @api string $content The content of the property 
  58. */ 
  59. $content = apply_filters( 'wpseo_og_' . $og_property, $content ); 
  60. if ( empty( $content ) ) { 
  61. return false; 
  62.  
  63. echo '<meta property="', esc_attr( $property ), '" content="', esc_attr( $content ), '" />', "\n"; 
  64.  
  65. return true; 
  66.  
  67. /** 
  68. * Filter the Facebook plugins metadata 
  69. * @param array $meta_tags the array to fix. 
  70. * @return array $meta_tags 
  71. */ 
  72. public function facebook_filter( $meta_tags ) { 
  73. $meta_tags['http://ogp.me/ns#type'] = $this->type( false ); 
  74. $meta_tags['http://ogp.me/ns#title'] = $this->og_title( false ); 
  75.  
  76. // Filter the locale too because the Facebook plugin locale code is not as good as ours. 
  77. $meta_tags['http://ogp.me/ns#locale'] = $this->locale( false ); 
  78.  
  79. $ogdesc = $this->description( false ); 
  80. if ( ! empty( $ogdesc ) ) { 
  81. $meta_tags['http://ogp.me/ns#description'] = $ogdesc; 
  82.  
  83. return $meta_tags; 
  84.  
  85. /** 
  86. * Filter for the namespace, adding the OpenGraph namespace. 
  87. * @link https://developers.facebook.com/docs/web/tutorials/scrumptious/open-graph-object/ 
  88. * @param string $input The input namespace string. 
  89. * @return string 
  90. */ 
  91. public function add_opengraph_namespace( $input ) { 
  92. $namespaces = array( 
  93. 'og: http://ogp.me/ns#',  
  94. ); 
  95. if ( $this->options['fbadminapp'] != 0 || ( is_array( $this->options['fb_admins'] ) && $this->options['fb_admins'] !== array() ) ) { 
  96. $namespaces[] = 'fb: http://ogp.me/ns/fb#'; 
  97.  
  98. /** 
  99. * Allow for adding additional namespaces to the <html> prefix attributes. 
  100. * @since 3.9.0 
  101. * @param array $namespaces Currently registered namespaces which are to be 
  102. * added to the prefix attribute. 
  103. * Namespaces are strings and have the following syntax: 
  104. * ns: http://url.to.namespace/definition 
  105. */ 
  106. $namespaces = apply_filters( 'wpseo_html_namespaces', $namespaces ); 
  107. $namespace_string = implode( ' ', array_unique( $namespaces ) ); 
  108.  
  109. if ( strpos( $input, ' prefix=' ) !== false ) { 
  110. $regex = '`prefix=([\'"])(.+?)\1`'; 
  111. $replace = 'prefix="$2 ' . $namespace_string . '"'; 
  112. $input = preg_replace( $regex, $replace, $input ); 
  113. else { 
  114. $input .= ' prefix="' . $namespace_string . '"'; 
  115.  
  116. return $input; 
  117.  
  118. /** 
  119. * Outputs the authors FB page. 
  120. * @link https://developers.facebook.com/blog/post/2013/06/19/platform-updates--new-open-graph-tags-for-media-publishers-and-more/ 
  121. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  122. * @return boolean 
  123. */ 
  124. public function article_author_facebook() { 
  125. if ( ! is_singular() ) { 
  126. return false; 
  127.  
  128. /** 
  129. * Filter: 'wpseo_opengraph_author_facebook' - Allow developers to filter the Yoast SEO post authors facebook profile URL 
  130. * @api bool|string $unsigned The Facebook author URL, return false to disable 
  131. */ 
  132. $facebook = apply_filters( 'wpseo_opengraph_author_facebook', get_the_author_meta( 'facebook', $GLOBALS['post']->post_author ) ); 
  133.  
  134. if ( $facebook && ( is_string( $facebook ) && $facebook !== '' ) ) { 
  135. $this->og_tag( 'article:author', $facebook ); 
  136.  
  137. return true; 
  138.  
  139. return false; 
  140.  
  141. /** 
  142. * Outputs the websites FB page. 
  143. * @link https://developers.facebook.com/blog/post/2013/06/19/platform-updates--new-open-graph-tags-for-media-publishers-and-more/ 
  144. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  145. * @return boolean 
  146. */ 
  147. public function website_facebook() { 
  148.  
  149. if ( 'article' === $this->type( false ) && ! empty( $this->options['facebook_site'] ) ) { 
  150. $this->og_tag( 'article:publisher', $this->options['facebook_site'] ); 
  151.  
  152. return true; 
  153.  
  154. return false; 
  155.  
  156. /** 
  157. * Outputs the site owner 
  158. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  159. * @return boolean 
  160. */ 
  161. public function site_owner() { 
  162. if ( isset( $this->options['fbadminapp'] ) && $this->options['fbadminapp'] != 0 ) { 
  163. $this->og_tag( 'fb:app_id', $this->options['fbadminapp'] ); 
  164.  
  165. return true; 
  166. else if ( isset( $this->options['fb_admins'] ) && is_array( $this->options['fb_admins'] ) && $this->options['fb_admins'] !== array() ) { 
  167. $adminstr = implode( ', ', array_keys( $this->options['fb_admins'] ) ); 
  168. /** 
  169. * Filter: 'wpseo_opengraph_admin' - Allow developer to filter the fb:admins string put out by Yoast SEO 
  170. * @api string $adminstr The admin string 
  171. */ 
  172. $adminstr = apply_filters( 'wpseo_opengraph_admin', $adminstr ); 
  173. if ( is_string( $adminstr ) && $adminstr !== '' ) { 
  174.  
  175. $admins = explode( ', ', $adminstr ); 
  176.  
  177. foreach ( $admins as $admin_id ) { 
  178. $this->og_tag( 'fb:admins', $admin_id ); 
  179.  
  180. return true; 
  181.  
  182. return false; 
  183.  
  184. /** 
  185. * Outputs the SEO title as OpenGraph title. 
  186. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  187. * @param bool $echo Whether or not to echo the output. 
  188. * @return string|boolean 
  189. */ 
  190. public function og_title( $echo = true ) { 
  191.  
  192. $frontend = WPSEO_Frontend::get_instance(); 
  193. $is_posts_page = $frontend->is_posts_page(); 
  194.  
  195. if ( is_singular() || $is_posts_page ) { 
  196.  
  197. $post_id = ( $is_posts_page ) ? get_option( 'page_for_posts' ) : get_the_ID(); 
  198. $post = get_post( $post_id ); 
  199. $title = WPSEO_Meta::get_value( 'opengraph-title', $post_id ); 
  200.  
  201. if ( $title === '' ) { 
  202. $title = $frontend->title( '' ); 
  203. else { 
  204. // Replace Yoast SEO Variables. 
  205. $title = wpseo_replace_vars( $title, $post ); 
  206. else if ( is_front_page() ) { 
  207. $title = ( isset( $this->options['og_frontpage_title'] ) && $this->options['og_frontpage_title'] !== '' ) ? $this->options['og_frontpage_title'] : $frontend->title( '' ); 
  208. elseif ( is_category() || is_tax() || is_tag() ) { 
  209. $title = WPSEO_Taxonomy_Meta::get_meta_without_term( 'opengraph-title' ); 
  210. if ( $title === '' ) { 
  211. $title = $frontend->title( '' ); 
  212. else { 
  213. // Replace Yoast SEO Variables. 
  214. $title = wpseo_replace_vars( $title, $GLOBALS['wp_query']->get_queried_object() ); 
  215. else { 
  216. $title = $frontend->title( '' ); 
  217.  
  218. /** 
  219. * Filter: 'wpseo_opengraph_title' - Allow changing the title specifically for OpenGraph 
  220. * @api string $unsigned The title string 
  221. */ 
  222. $title = trim( apply_filters( 'wpseo_opengraph_title', $title ) ); 
  223.  
  224. if ( is_string( $title ) && $title !== '' ) { 
  225. if ( $echo !== false ) { 
  226. $this->og_tag( 'og:title', $title ); 
  227.  
  228. return true; 
  229.  
  230. if ( $echo === false ) { 
  231. return $title; 
  232.  
  233. return false; 
  234.  
  235. /** 
  236. * Outputs the canonical URL as OpenGraph URL, which consolidates likes and shares. 
  237. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  238. * @return boolean 
  239. */ 
  240. public function url() { 
  241. /** 
  242. * Filter: 'wpseo_opengraph_url' - Allow changing the OpenGraph URL 
  243. * @api string $unsigned Canonical URL 
  244. */ 
  245. $url = apply_filters( 'wpseo_opengraph_url', WPSEO_Frontend::get_instance()->canonical( false ) ); 
  246.  
  247. if ( is_string( $url ) && $url !== '' ) { 
  248. $this->og_tag( 'og:url', esc_url( $url ) ); 
  249.  
  250. return true; 
  251.  
  252. return false; 
  253.  
  254. /** 
  255. * Output the locale, doing some conversions to make sure the proper Facebook locale is outputted. 
  256. * Last update/compare with FB list done on 2015-03-16 by Rarst 
  257. * @see http://www.facebook.com/translations/FacebookLocales.xml for the list of supported locales 
  258. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  259. * @param bool $echo Whether to echo or return the locale. 
  260. * @return string $locale 
  261. */ 
  262. public function locale( $echo = true ) { 
  263. /** 
  264. * Filter: 'wpseo_locale' - Allow changing the locale output 
  265. * @api string $unsigned Locale string 
  266. */ 
  267. $locale = apply_filters( 'wpseo_locale', get_locale() ); 
  268.  
  269. // Catch some weird locales served out by WP that are not easily doubled up. 
  270. $fix_locales = array( 
  271. 'ca' => 'ca_ES',  
  272. 'en' => 'en_US',  
  273. 'el' => 'el_GR',  
  274. 'et' => 'et_EE',  
  275. 'ja' => 'ja_JP',  
  276. 'sq' => 'sq_AL',  
  277. 'uk' => 'uk_UA',  
  278. 'vi' => 'vi_VN',  
  279. 'zh' => 'zh_CN',  
  280. ); 
  281.  
  282. if ( isset( $fix_locales[ $locale ] ) ) { 
  283. $locale = $fix_locales[ $locale ]; 
  284.  
  285. // Convert locales like "es" to "es_ES", in case that works for the given locale (sometimes it does). 
  286. if ( strlen( $locale ) == 2 ) { 
  287. $locale = strtolower( $locale ) . '_' . strtoupper( $locale ); 
  288.  
  289. // These are the locales FB supports. 
  290. $fb_valid_fb_locales = array( 
  291. 'af_ZA', // Afrikaans. 
  292. 'ak_GH', // Akan. 
  293. 'am_ET', // Amharic. 
  294. 'ar_AR', // Arabic. 
  295. 'as_IN', // Assamese. 
  296. 'ay_BO', // Aymara. 
  297. 'az_AZ', // Azerbaijani. 
  298. 'be_BY', // Belarusian. 
  299. 'bg_BG', // Bulgarian. 
  300. 'bn_IN', // Bengali. 
  301. 'br_FR', // Breton. 
  302. 'bs_BA', // Bosnian. 
  303. 'ca_ES', // Catalan. 
  304. 'cb_IQ', // Sorani Kurdish. 
  305. 'ck_US', // Cherokee. 
  306. 'co_FR', // Corsican. 
  307. 'cs_CZ', // Czech. 
  308. 'cx_PH', // Cebuano. 
  309. 'cy_GB', // Welsh. 
  310. 'da_DK', // Danish. 
  311. 'de_DE', // German. 
  312. 'el_GR', // Greek. 
  313. 'en_GB', // English (UK). 
  314. 'en_IN', // English (India). 
  315. 'en_PI', // English (Pirate). 
  316. 'en_UD', // English (Upside Down). 
  317. 'en_US', // English (US). 
  318. 'eo_EO', // Esperanto. 
  319. 'es_CL', // Spanish (Chile). 
  320. 'es_CO', // Spanish (Colombia). 
  321. 'es_ES', // Spanish (Spain). 
  322. 'es_LA', // Spanish. 
  323. 'es_MX', // Spanish (Mexico). 
  324. 'es_VE', // Spanish (Venezuela). 
  325. 'et_EE', // Estonian. 
  326. 'eu_ES', // Basque. 
  327. 'fa_IR', // Persian. 
  328. 'fb_LT', // Leet Speak. 
  329. 'ff_NG', // Fulah. 
  330. 'fi_FI', // Finnish. 
  331. 'fo_FO', // Faroese. 
  332. 'fr_CA', // French (Canada). 
  333. 'fr_FR', // French (France). 
  334. 'fy_NL', // Frisian. 
  335. 'ga_IE', // Irish. 
  336. 'gl_ES', // Galician. 
  337. 'gn_PY', // Guarani. 
  338. 'gu_IN', // Gujarati. 
  339. 'gx_GR', // Classical Greek. 
  340. 'ha_NG', // Hausa. 
  341. 'he_IL', // Hebrew. 
  342. 'hi_IN', // Hindi. 
  343. 'hr_HR', // Croatian. 
  344. 'hu_HU', // Hungarian. 
  345. 'hy_AM', // Armenian. 
  346. 'id_ID', // Indonesian. 
  347. 'ig_NG', // Igbo. 
  348. 'is_IS', // Icelandic. 
  349. 'it_IT', // Italian. 
  350. 'ja_JP', // Japanese. 
  351. 'ja_KS', // Japanese (Kansai). 
  352. 'jv_ID', // Javanese. 
  353. 'ka_GE', // Georgian. 
  354. 'kk_KZ', // Kazakh. 
  355. 'km_KH', // Khmer. 
  356. 'kn_IN', // Kannada. 
  357. 'ko_KR', // Korean. 
  358. 'ku_TR', // Kurdish (Kurmanji). 
  359. 'ky_KG', // Kyrgyz. 
  360. 'la_VA', // Latin. 
  361. 'lg_UG', // Ganda. 
  362. 'li_NL', // Limburgish. 
  363. 'ln_CD', // Lingala. 
  364. 'lo_LA', // Lao. 
  365. 'lt_LT', // Lithuanian. 
  366. 'lv_LV', // Latvian. 
  367. 'mg_MG', // Malagasy. 
  368. 'mi_NZ', // Maori. 
  369. 'mk_MK', // Macedonian. 
  370. 'ml_IN', // Malayalam. 
  371. 'mn_MN', // Mongolian. 
  372. 'mr_IN', // Marathi. 
  373. 'ms_MY', // Malay. 
  374. 'mt_MT', // Maltese. 
  375. 'my_MM', // Burmese. 
  376. 'nb_NO', // Norwegian (bokmal). 
  377. 'nd_ZW', // Ndebele. 
  378. 'ne_NP', // Nepali. 
  379. 'nl_BE', // Dutch (Belgie). 
  380. 'nl_NL', // Dutch. 
  381. 'nn_NO', // Norwegian (nynorsk). 
  382. 'ny_MW', // Chewa. 
  383. 'or_IN', // Oriya. 
  384. 'pa_IN', // Punjabi. 
  385. 'pl_PL', // Polish. 
  386. 'ps_AF', // Pashto. 
  387. 'pt_BR', // Portuguese (Brazil). 
  388. 'pt_PT', // Portuguese (Portugal). 
  389. 'qu_PE', // Quechua. 
  390. 'rm_CH', // Romansh. 
  391. 'ro_RO', // Romanian. 
  392. 'ru_RU', // Russian. 
  393. 'rw_RW', // Kinyarwanda. 
  394. 'sa_IN', // Sanskrit. 
  395. 'sc_IT', // Sardinian. 
  396. 'se_NO', // Northern Sami. 
  397. 'si_LK', // Sinhala. 
  398. 'sk_SK', // Slovak. 
  399. 'sl_SI', // Slovenian. 
  400. 'sn_ZW', // Shona. 
  401. 'so_SO', // Somali. 
  402. 'sq_AL', // Albanian. 
  403. 'sr_RS', // Serbian. 
  404. 'sv_SE', // Swedish. 
  405. 'sw_KE', // Swahili. 
  406. 'sy_SY', // Syriac. 
  407. 'sz_PL', // Silesian. 
  408. 'ta_IN', // Tamil. 
  409. 'te_IN', // Telugu. 
  410. 'tg_TJ', // Tajik. 
  411. 'th_TH', // Thai. 
  412. 'tk_TM', // Turkmen. 
  413. 'tl_PH', // Filipino. 
  414. 'tl_ST', // Klingon. 
  415. 'tr_TR', // Turkish. 
  416. 'tt_RU', // Tatar. 
  417. 'tz_MA', // Tamazight. 
  418. 'uk_UA', // Ukrainian. 
  419. 'ur_PK', // Urdu. 
  420. 'uz_UZ', // Uzbek. 
  421. 'vi_VN', // Vietnamese. 
  422. 'wo_SN', // Wolof. 
  423. 'xh_ZA', // Xhosa. 
  424. 'yi_DE', // Yiddish. 
  425. 'yo_NG', // Yoruba. 
  426. 'zh_CN', // Simplified Chinese (China). 
  427. 'zh_HK', // Traditional Chinese (Hong Kong). 
  428. 'zh_TW', // Traditional Chinese (Taiwan). 
  429. 'zu_ZA', // Zulu. 
  430. 'zz_TR', // Zazaki. 
  431. ); 
  432.  
  433. // Check to see if the locale is a valid FB one, if not, use en_US as a fallback. 
  434. if ( ! in_array( $locale, $fb_valid_fb_locales ) ) { 
  435. $locale = strtolower( substr( $locale, 0, 2 ) ) . '_' . strtoupper( substr( $locale, 0, 2 ) ); 
  436. if ( ! in_array( $locale, $fb_valid_fb_locales ) ) { 
  437. $locale = 'en_US'; 
  438.  
  439. if ( $echo !== false ) { 
  440. $this->og_tag( 'og:locale', $locale ); 
  441.  
  442. return $locale; 
  443.  
  444. /** 
  445. * Output the OpenGraph type. 
  446. * @param boolean $echo Whether to echo or return the type. 
  447. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/object/ 
  448. * @return string $type 
  449. */ 
  450. public function type( $echo = true ) { 
  451.  
  452. if ( is_front_page() || is_home() ) { 
  453. $type = 'website'; 
  454. elseif ( is_singular() ) { 
  455.  
  456. // This'll usually only be changed by plugins right now. 
  457. $type = WPSEO_Meta::get_value( 'og_type' ); 
  458.  
  459. if ( $type === '' ) { 
  460. $type = 'article'; 
  461. else { 
  462. // We use "object" for archives etc. as article doesn't apply there. 
  463. $type = 'object'; 
  464.  
  465. /** 
  466. * Filter: 'wpseo_opengraph_type' - Allow changing the OpenGraph type of the page 
  467. * @api string $type The OpenGraph type string. 
  468. */ 
  469. $type = apply_filters( 'wpseo_opengraph_type', $type ); 
  470.  
  471. if ( is_string( $type ) && $type !== '' ) { 
  472. if ( $echo !== false ) { 
  473. $this->og_tag( 'og:type', $type ); 
  474. else { 
  475. return $type; 
  476.  
  477. return ''; 
  478.  
  479. /** 
  480. * Create new WPSEO_OpenGraph_Image class and get the images to set the og:image 
  481. * @param string|boolean $image Optional image URL. 
  482. */ 
  483. public function image( $image = false ) { 
  484. $opengraph_images = new WPSEO_OpenGraph_Image( $this->options, $image ); 
  485.  
  486. foreach ( $opengraph_images->get_images() as $img ) { 
  487. $this->og_tag( 'og:image', esc_url( $img ) ); 
  488.  
  489. $dimensions = $opengraph_images->get_dimensions(); 
  490.  
  491. if ( ! empty( $dimensions['width'] ) ) { 
  492. $this->og_tag( 'og:image:width', absint( $dimensions['width'] ) ); 
  493.  
  494. if ( ! empty( $dimensions['height'] ) ) { 
  495. $this->og_tag( 'og:image:height', absint( $dimensions['height'] ) ); 
  496.  
  497. /** 
  498. * Fallback method for plugins using image_output 
  499. * @param string $image Image URL. 
  500. */ 
  501. public function image_output( $image ) { 
  502. $this->image( $image ); 
  503.  
  504. /** 
  505. * Output the OpenGraph description, specific OG description first, if not, grab the meta description. 
  506. * @param bool $echo Whether to echo or return the description. 
  507. * @return string $ogdesc 
  508. */ 
  509. public function description( $echo = true ) { 
  510. $ogdesc = ''; 
  511. $frontend = WPSEO_Frontend::get_instance(); 
  512.  
  513. if ( is_front_page() ) { 
  514. if ( isset( $this->options['og_frontpage_desc'] ) && $this->options['og_frontpage_desc'] !== '' ) { 
  515. $ogdesc = wpseo_replace_vars( $this->options['og_frontpage_desc'], null ); 
  516. else { 
  517. $ogdesc = $frontend->metadesc( false ); 
  518.  
  519. $is_posts_page = $frontend->is_posts_page(); 
  520.  
  521. if ( is_singular() || $is_posts_page ) { 
  522. $post_id = ( $is_posts_page ) ? get_option( 'page_for_posts' ) : get_the_ID(); 
  523. $post = get_post( $post_id ); 
  524. $ogdesc = WPSEO_Meta::get_value( 'opengraph-description', $post_id ); 
  525.  
  526. // Replace Yoast SEO Variables. 
  527. $ogdesc = wpseo_replace_vars( $ogdesc, $post ); 
  528.  
  529. // Use metadesc if $ogdesc is empty. 
  530. if ( $ogdesc === '' ) { 
  531. $ogdesc = $frontend->metadesc( false ); 
  532.  
  533. // Tag og:description is still blank so grab it from get_the_excerpt(). 
  534. if ( ! is_string( $ogdesc ) || ( is_string( $ogdesc ) && $ogdesc === '' ) ) { 
  535. $ogdesc = str_replace( '[…]', '…', strip_tags( get_the_excerpt() ) ); 
  536.  
  537. if ( is_category() || is_tag() || is_tax() ) { 
  538. $ogdesc = WPSEO_Taxonomy_Meta::get_meta_without_term( 'opengraph-description' ); 
  539. if ( $ogdesc === '' ) { 
  540. $ogdesc = $frontend->metadesc( false ); 
  541.  
  542. if ( $ogdesc === '' ) { 
  543. $ogdesc = trim( strip_tags( term_description() ) ); 
  544.  
  545. if ( $ogdesc === '' ) { 
  546. $ogdesc = WPSEO_Taxonomy_Meta::get_meta_without_term( 'desc' ); 
  547.  
  548. // Strip shortcodes if any. 
  549. $ogdesc = strip_shortcodes( $ogdesc ); 
  550.  
  551. /** 
  552. * Filter: 'wpseo_opengraph_desc' - Allow changing the OpenGraph description 
  553. * @api string $ogdesc The description string. 
  554. */ 
  555. $ogdesc = trim( apply_filters( 'wpseo_opengraph_desc', $ogdesc ) ); 
  556.  
  557. if ( is_string( $ogdesc ) && $ogdesc !== '' ) { 
  558. if ( $echo !== false ) { 
  559. $this->og_tag( 'og:description', $ogdesc ); 
  560.  
  561. return $ogdesc; 
  562.  
  563. /** 
  564. * Output the site name straight from the blog info. 
  565. */ 
  566. public function site_name() { 
  567. /** 
  568. * Filter: 'wpseo_opengraph_site_name' - Allow changing the OpenGraph site name 
  569. * @api string $unsigned Blog name string 
  570. */ 
  571. $name = apply_filters( 'wpseo_opengraph_site_name', get_bloginfo( 'name' ) ); 
  572. if ( is_string( $name ) && $name !== '' ) { 
  573. $this->og_tag( 'og:site_name', $name ); 
  574.  
  575. /** 
  576. * Output the article tags as article:tag tags. 
  577. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  578. * @return boolean 
  579. */ 
  580. public function tags() { 
  581. if ( ! is_singular() ) { 
  582. return false; 
  583.  
  584. $tags = get_the_tags(); 
  585. if ( ! is_wp_error( $tags ) && ( is_array( $tags ) && $tags !== array() ) ) { 
  586.  
  587. foreach ( $tags as $tag ) { 
  588. $this->og_tag( 'article:tag', $tag->name ); 
  589.  
  590. return true; 
  591.  
  592. return false; 
  593.  
  594. /** 
  595. * Output the article category as an article:section tag. 
  596. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  597. * @return boolean; 
  598. */ 
  599. public function category() { 
  600.  
  601. if ( ! is_singular() ) { 
  602. return false; 
  603.  
  604. $terms = get_the_category(); 
  605.  
  606. if ( ! is_wp_error( $terms ) && ( is_array( $terms ) && $terms !== array() ) ) { 
  607.  
  608. // We can only show one section here, so we take the first one. 
  609. $this->og_tag( 'article:section', $terms[0]->name ); 
  610.  
  611. return true; 
  612.  
  613. return false; 
  614.  
  615. /** 
  616. * Output the article publish and last modification date 
  617. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/ 
  618. * @return boolean; 
  619. */ 
  620. public function publish_date() { 
  621.  
  622. if ( ! is_singular( 'post' ) ) { 
  623. /** 
  624. * Filter: 'wpseo_opengraph_show_publish_date' - Allow showing publication date for other post types 
  625. * @api bool $unsigned Whether or not to show publish date 
  626. * @param string $post_type The current URL's post type. 
  627. */ 
  628. if ( false === apply_filters( 'wpseo_opengraph_show_publish_date', false, get_post_type() ) ) { 
  629. return false; 
  630.  
  631. $pub = get_the_date( DATE_W3C ); 
  632. $this->og_tag( 'article:published_time', $pub ); 
  633.  
  634. $mod = get_the_modified_date( DATE_W3C ); 
  635. if ( $mod != $pub ) { 
  636. $this->og_tag( 'article:modified_time', $mod ); 
  637. $this->og_tag( 'og:updated_time', $mod ); 
  638.  
  639. return true; 
  640. } /** End of class */