class-opengraph.php 20 KB

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