class-opengraph.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. /** @var WPSEO_Frontend_Page_Type */
  12. protected $frontend_page_type;
  13. /**
  14. * Class constructor.
  15. */
  16. public function __construct() {
  17. if ( isset( $GLOBALS['fb_ver'] ) || class_exists( 'Facebook_Loader', false ) ) {
  18. add_filter( 'fb_meta_tags', array( $this, 'facebook_filter' ), 10, 1 );
  19. }
  20. else {
  21. add_filter( 'language_attributes', array( $this, 'add_opengraph_namespace' ), 15 );
  22. add_action( 'wpseo_opengraph', array( $this, 'locale' ), 1 );
  23. add_action( 'wpseo_opengraph', array( $this, 'type' ), 5 );
  24. add_action( 'wpseo_opengraph', array( $this, 'og_title' ), 10 );
  25. add_action( 'wpseo_opengraph', array( $this, 'app_id' ), 20 );
  26. add_action( 'wpseo_opengraph', array( $this, 'description' ), 11 );
  27. add_action( 'wpseo_opengraph', array( $this, 'url' ), 12 );
  28. add_action( 'wpseo_opengraph', array( $this, 'site_name' ), 13 );
  29. add_action( 'wpseo_opengraph', array( $this, 'website_facebook' ), 14 );
  30. if ( is_singular() && ! is_front_page() ) {
  31. add_action( 'wpseo_opengraph', array( $this, 'article_author_facebook' ), 15 );
  32. add_action( 'wpseo_opengraph', array( $this, 'tags' ), 16 );
  33. add_action( 'wpseo_opengraph', array( $this, 'category' ), 17 );
  34. add_action( 'wpseo_opengraph', array( $this, 'publish_date' ), 19 );
  35. }
  36. add_action( 'wpseo_opengraph', array( $this, 'image' ), 30 );
  37. }
  38. add_filter( 'jetpack_enable_open_graph', '__return_false' );
  39. add_action( 'wpseo_head', array( $this, 'opengraph' ), 30 );
  40. // Class for determine the current page type.
  41. $this->frontend_page_type = new WPSEO_Frontend_Page_Type();
  42. }
  43. /**
  44. * Main OpenGraph output.
  45. */
  46. public function opengraph() {
  47. wp_reset_query();
  48. /**
  49. * Action: 'wpseo_opengraph' - Hook to add all Facebook OpenGraph output to so they're close together.
  50. */
  51. do_action( 'wpseo_opengraph' );
  52. }
  53. /**
  54. * Internal function to output FB tags. This also adds an output filter to each bit of output based on the property.
  55. *
  56. * @param string $property Property attribute value.
  57. * @param string $content Content attribute value.
  58. *
  59. * @return boolean
  60. */
  61. public function og_tag( $property, $content ) {
  62. $og_property = str_replace( ':', '_', $property );
  63. /**
  64. * Filter: 'wpseo_og_' . $og_property - Allow developers to change the content of specific OG meta tags.
  65. *
  66. * @api string $content The content of the property.
  67. */
  68. $content = apply_filters( 'wpseo_og_' . $og_property, $content );
  69. if ( empty( $content ) ) {
  70. return false;
  71. }
  72. echo '<meta property="', esc_attr( $property ), '" content="', esc_attr( $content ), '" />', "\n";
  73. return true;
  74. }
  75. /**
  76. * Filter the Facebook plugins metadata.
  77. *
  78. * @param array $meta_tags The array to fix.
  79. *
  80. * @return array $meta_tags
  81. */
  82. public function facebook_filter( $meta_tags ) {
  83. $meta_tags['http://ogp.me/ns#type'] = $this->type( false );
  84. $meta_tags['http://ogp.me/ns#title'] = $this->og_title( false );
  85. // Filter the locale too because the Facebook plugin locale code is not as good as ours.
  86. $meta_tags['http://ogp.me/ns#locale'] = $this->locale( false );
  87. $ogdesc = $this->description( false );
  88. if ( ! empty( $ogdesc ) ) {
  89. $meta_tags['http://ogp.me/ns#description'] = $ogdesc;
  90. }
  91. return $meta_tags;
  92. }
  93. /**
  94. * Filter for the namespace, adding the OpenGraph namespace.
  95. *
  96. * @link https://developers.facebook.com/docs/web/tutorials/scrumptious/open-graph-object/
  97. *
  98. * @param string $input The input namespace string.
  99. *
  100. * @return string
  101. */
  102. public function add_opengraph_namespace( $input ) {
  103. $namespaces = array(
  104. 'og: http://ogp.me/ns#',
  105. );
  106. /**
  107. * Allow for adding additional namespaces to the <html> prefix attributes.
  108. *
  109. * @since 3.9.0
  110. *
  111. * @param array $namespaces Currently registered namespaces which are to be
  112. * added to the prefix attribute.
  113. * Namespaces are strings and have the following syntax:
  114. * ns: http://url.to.namespace/definition
  115. */
  116. $namespaces = apply_filters( 'wpseo_html_namespaces', $namespaces );
  117. $namespace_string = implode( ' ', array_unique( $namespaces ) );
  118. if ( strpos( $input, ' prefix=' ) !== false ) {
  119. $regex = '`prefix=([\'"])(.+?)\1`';
  120. $replace = 'prefix="$2 ' . $namespace_string . '"';
  121. $input = preg_replace( $regex, $replace, $input );
  122. }
  123. else {
  124. $input .= ' prefix="' . $namespace_string . '"';
  125. }
  126. return $input;
  127. }
  128. /**
  129. * Outputs the authors FB page.
  130. *
  131. * @link https://developers.facebook.com/blog/post/2013/06/19/platform-updates--new-open-graph-tags-for-media-publishers-and-more/
  132. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  133. *
  134. * @return boolean
  135. */
  136. public function article_author_facebook() {
  137. if ( ! is_singular() ) {
  138. return false;
  139. }
  140. /**
  141. * Filter: 'wpseo_opengraph_author_facebook' - Allow developers to filter the Yoast SEO post authors facebook profile URL.
  142. *
  143. * @api bool|string $unsigned The Facebook author URL, return false to disable.
  144. */
  145. $facebook = apply_filters( 'wpseo_opengraph_author_facebook', get_the_author_meta( 'facebook', $GLOBALS['post']->post_author ) );
  146. if ( $facebook && ( is_string( $facebook ) && $facebook !== '' ) ) {
  147. $this->og_tag( 'article:author', $facebook );
  148. return true;
  149. }
  150. return false;
  151. }
  152. /**
  153. * Outputs the websites FB page.
  154. *
  155. * @link https://developers.facebook.com/blog/post/2013/06/19/platform-updates--new-open-graph-tags-for-media-publishers-and-more/
  156. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  157. * @return boolean
  158. */
  159. public function website_facebook() {
  160. if ( 'article' === $this->type( false ) && WPSEO_Options::get( 'facebook_site', '' ) !== '' ) {
  161. $this->og_tag( 'article:publisher', WPSEO_Options::get( 'facebook_site' ) );
  162. return true;
  163. }
  164. return false;
  165. }
  166. /**
  167. * Outputs the SEO title as OpenGraph title.
  168. *
  169. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  170. *
  171. * @param bool $echo Whether or not to echo the output.
  172. *
  173. * @return string|boolean
  174. */
  175. public function og_title( $echo = true ) {
  176. $frontend = WPSEO_Frontend::get_instance();
  177. if ( $this->frontend_page_type->is_simple_page() ) {
  178. $post_id = $this->frontend_page_type->get_simple_page_id();
  179. $post = get_post( $post_id );
  180. $title = WPSEO_Meta::get_value( 'opengraph-title', $post_id );
  181. if ( $title === '' ) {
  182. $title = $frontend->title( '' );
  183. }
  184. else {
  185. // Replace Yoast SEO Variables.
  186. $title = wpseo_replace_vars( $title, $post );
  187. }
  188. }
  189. elseif ( is_front_page() ) {
  190. $title = ( WPSEO_Options::get( 'og_frontpage_title', '' ) !== '' ) ? WPSEO_Options::get( 'og_frontpage_title' ) : $frontend->title( '' );
  191. }
  192. elseif ( is_category() || is_tax() || is_tag() ) {
  193. $title = WPSEO_Taxonomy_Meta::get_meta_without_term( 'opengraph-title' );
  194. if ( $title === '' ) {
  195. $title = $frontend->title( '' );
  196. }
  197. else {
  198. // Replace Yoast SEO Variables.
  199. $title = wpseo_replace_vars( $title, $GLOBALS['wp_query']->get_queried_object() );
  200. }
  201. }
  202. else {
  203. $title = $frontend->title( '' );
  204. }
  205. /**
  206. * Filter: 'wpseo_opengraph_title' - Allow changing the title specifically for OpenGraph.
  207. *
  208. * @api string $unsigned The title string.
  209. */
  210. $title = trim( apply_filters( 'wpseo_opengraph_title', $title ) );
  211. if ( is_string( $title ) && $title !== '' ) {
  212. if ( $echo !== false ) {
  213. $this->og_tag( 'og:title', $title );
  214. return true;
  215. }
  216. }
  217. if ( $echo === false ) {
  218. return $title;
  219. }
  220. return false;
  221. }
  222. /**
  223. * Outputs the canonical URL as OpenGraph URL, which consolidates likes and shares.
  224. *
  225. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  226. * @return boolean
  227. */
  228. public function url() {
  229. $url = WPSEO_Frontend::get_instance()->canonical( false, false );
  230. $unpaged_url = WPSEO_Frontend::get_instance()->canonical( false, true );
  231. /*
  232. * If the unpaged URL is the same as the normal URL but just with pagination added, use that.
  233. * This makes sure we always use the unpaged URL when we can, but doesn't break for overridden canonicals.
  234. */
  235. if ( is_string( $unpaged_url ) && strpos( $url, $unpaged_url ) === 0 ) {
  236. $url = $unpaged_url;
  237. }
  238. /**
  239. * Filter: 'wpseo_opengraph_url' - Allow changing the OpenGraph URL.
  240. *
  241. * @api string $unsigned Canonical URL.
  242. */
  243. $url = apply_filters( 'wpseo_opengraph_url', $url );
  244. if ( is_string( $url ) && $url !== '' ) {
  245. $this->og_tag( 'og:url', esc_url( $url ) );
  246. return true;
  247. }
  248. return false;
  249. }
  250. /**
  251. * Output the locale, doing some conversions to make sure the proper Facebook locale is outputted.
  252. *
  253. * Last update/compare with FB list done on 2015-03-16 by Rarst
  254. *
  255. * @see http://www.facebook.com/translations/FacebookLocales.xml for the list of supported locales
  256. *
  257. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  258. *
  259. * @param bool $echo Whether to echo or return the locale.
  260. *
  261. * @return string $locale
  262. */
  263. public function locale( $echo = true ) {
  264. /**
  265. * Filter: 'wpseo_locale' - Allow changing the locale output.
  266. *
  267. * @api string $unsigned Locale string.
  268. */
  269. $locale = apply_filters( 'wpseo_locale', get_locale() );
  270. // Catch some weird locales served out by WP that are not easily doubled up.
  271. $fix_locales = array(
  272. 'ca' => 'ca_ES',
  273. 'en' => 'en_US',
  274. 'el' => 'el_GR',
  275. 'et' => 'et_EE',
  276. 'ja' => 'ja_JP',
  277. 'sq' => 'sq_AL',
  278. 'uk' => 'uk_UA',
  279. 'vi' => 'vi_VN',
  280. 'zh' => 'zh_CN',
  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. // Check to see if the locale is a valid FB one, if not, use en_US as a fallback.
  433. if ( ! in_array( $locale, $fb_valid_fb_locales, true ) ) {
  434. $locale = strtolower( substr( $locale, 0, 2 ) ) . '_' . strtoupper( substr( $locale, 0, 2 ) );
  435. if ( ! in_array( $locale, $fb_valid_fb_locales, true ) ) {
  436. $locale = 'en_US';
  437. }
  438. }
  439. if ( $echo !== false ) {
  440. $this->og_tag( 'og:locale', $locale );
  441. }
  442. return $locale;
  443. }
  444. /**
  445. * Output the OpenGraph type.
  446. *
  447. * @param boolean $echo Whether to echo or return the type.
  448. *
  449. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/object/
  450. *
  451. * @return string $type
  452. */
  453. public function type( $echo = true ) {
  454. if ( is_front_page() || is_home() ) {
  455. $type = 'website';
  456. }
  457. elseif ( is_singular() ) {
  458. // This'll usually only be changed by plugins right now.
  459. $type = WPSEO_Meta::get_value( 'og_type' );
  460. if ( $type === '' ) {
  461. $type = 'article';
  462. }
  463. }
  464. else {
  465. // We use "object" for archives etc. as article doesn't apply there.
  466. $type = 'object';
  467. }
  468. /**
  469. * Filter: 'wpseo_opengraph_type' - Allow changing the OpenGraph type of the page.
  470. *
  471. * @api string $type The OpenGraph type string.
  472. */
  473. $type = apply_filters( 'wpseo_opengraph_type', $type );
  474. if ( is_string( $type ) && $type !== '' ) {
  475. if ( $echo !== false ) {
  476. $this->og_tag( 'og:type', $type );
  477. }
  478. else {
  479. return $type;
  480. }
  481. }
  482. return '';
  483. }
  484. /**
  485. * Create new WPSEO_OpenGraph_Image class and get the images to set the og:image.
  486. *
  487. * @param string|bool $image Optional. Image URL.
  488. *
  489. * @return void
  490. */
  491. public function image( $image = false ) {
  492. $opengraph_image = new WPSEO_OpenGraph_Image( $image, $this );
  493. $opengraph_image->show();
  494. }
  495. /**
  496. * Output the OpenGraph description, specific OG description first, if not, grab the meta description.
  497. *
  498. * @param bool $echo Whether to echo or return the description.
  499. *
  500. * @return string $ogdesc
  501. */
  502. public function description( $echo = true ) {
  503. $ogdesc = '';
  504. $frontend = WPSEO_Frontend::get_instance();
  505. if ( is_front_page() ) {
  506. if ( WPSEO_Options::get( 'og_frontpage_desc', '' ) !== '' ) {
  507. $ogdesc = wpseo_replace_vars( WPSEO_Options::get( 'og_frontpage_desc' ), null );
  508. }
  509. else {
  510. $ogdesc = $frontend->metadesc( false );
  511. }
  512. }
  513. if ( $this->frontend_page_type->is_simple_page() ) {
  514. $post_id = $this->frontend_page_type->get_simple_page_id();
  515. $post = get_post( $post_id );
  516. $ogdesc = WPSEO_Meta::get_value( 'opengraph-description', $post_id );
  517. // Replace Yoast SEO Variables.
  518. $ogdesc = wpseo_replace_vars( $ogdesc, $post );
  519. // Use metadesc if $ogdesc is empty.
  520. if ( $ogdesc === '' ) {
  521. $ogdesc = $frontend->metadesc( false );
  522. }
  523. // Tag og:description is still blank so grab it from get_the_excerpt().
  524. if ( ! is_string( $ogdesc ) || ( is_string( $ogdesc ) && $ogdesc === '' ) ) {
  525. $ogdesc = str_replace( '[&hellip;]', '&hellip;', wp_strip_all_tags( get_the_excerpt() ) );
  526. }
  527. }
  528. if ( is_category() || is_tag() || is_tax() ) {
  529. $ogdesc = WPSEO_Taxonomy_Meta::get_meta_without_term( 'opengraph-description' );
  530. if ( $ogdesc === '' ) {
  531. $ogdesc = $frontend->metadesc( false );
  532. }
  533. if ( $ogdesc === '' ) {
  534. $ogdesc = wp_strip_all_tags( term_description() );
  535. }
  536. if ( $ogdesc === '' ) {
  537. $ogdesc = WPSEO_Taxonomy_Meta::get_meta_without_term( 'desc' );
  538. }
  539. $ogdesc = wpseo_replace_vars( $ogdesc, get_queried_object() );
  540. }
  541. // Strip shortcodes if any.
  542. $ogdesc = strip_shortcodes( $ogdesc );
  543. /**
  544. * Filter: 'wpseo_opengraph_desc' - Allow changing the OpenGraph description.
  545. *
  546. * @api string $ogdesc The description string.
  547. */
  548. $ogdesc = trim( apply_filters( 'wpseo_opengraph_desc', $ogdesc ) );
  549. if ( is_string( $ogdesc ) && $ogdesc !== '' ) {
  550. if ( $echo !== false ) {
  551. $this->og_tag( 'og:description', $ogdesc );
  552. }
  553. }
  554. return $ogdesc;
  555. }
  556. /**
  557. * Output the site name straight from the blog info.
  558. */
  559. public function site_name() {
  560. /**
  561. * Filter: 'wpseo_opengraph_site_name' - Allow changing the OpenGraph site name.
  562. *
  563. * @api string $unsigned Blog name string.
  564. */
  565. $name = apply_filters( 'wpseo_opengraph_site_name', get_bloginfo( 'name' ) );
  566. if ( is_string( $name ) && $name !== '' ) {
  567. $this->og_tag( 'og:site_name', $name );
  568. }
  569. }
  570. /**
  571. * Output the article tags as article:tag tags.
  572. *
  573. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  574. * @return boolean
  575. */
  576. public function tags() {
  577. if ( ! is_singular() ) {
  578. return false;
  579. }
  580. $tags = get_the_tags();
  581. if ( ! is_wp_error( $tags ) && ( is_array( $tags ) && $tags !== array() ) ) {
  582. foreach ( $tags as $tag ) {
  583. $this->og_tag( 'article:tag', $tag->name );
  584. }
  585. return true;
  586. }
  587. return false;
  588. }
  589. /**
  590. * Output the article category as an article:section tag.
  591. *
  592. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  593. * @return boolean;
  594. */
  595. public function category() {
  596. if ( ! is_singular() ) {
  597. return false;
  598. }
  599. $post = get_post();
  600. if ( ! $post ) {
  601. return false;
  602. }
  603. $primary_term = new WPSEO_Primary_Term( 'category', $post->ID );
  604. $primary_category = $primary_term->get_primary_term();
  605. if ( $primary_category ) {
  606. // We can only show one section here, so we take the first one.
  607. $this->og_tag( 'article:section', get_cat_name( $primary_category ) );
  608. return true;
  609. }
  610. $terms = get_the_category();
  611. if ( ! is_wp_error( $terms ) && is_array( $terms ) && ! empty( $terms ) ) {
  612. // We can only show one section here, so we take the first one.
  613. $term = reset( $terms );
  614. $this->og_tag( 'article:section', $term->name );
  615. return true;
  616. }
  617. return false;
  618. }
  619. /**
  620. * Output the article publish and last modification date.
  621. *
  622. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  623. * @return boolean;
  624. */
  625. public function publish_date() {
  626. if ( ! is_singular( 'post' ) ) {
  627. /**
  628. * Filter: 'wpseo_opengraph_show_publish_date' - Allow showing publication date for other post types.
  629. *
  630. * @api bool $unsigned Whether or not to show publish date.
  631. *
  632. * @param string $post_type The current URL's post type.
  633. */
  634. if ( false === apply_filters( 'wpseo_opengraph_show_publish_date', false, get_post_type() ) ) {
  635. return false;
  636. }
  637. }
  638. $post = get_post();
  639. $pub = mysql2date( DATE_W3C, $post->post_date_gmt, false );
  640. $this->og_tag( 'article:published_time', $pub );
  641. $mod = mysql2date( DATE_W3C, $post->post_modified_gmt, false );
  642. if ( $mod !== $pub ) {
  643. $this->og_tag( 'article:modified_time', $mod );
  644. $this->og_tag( 'og:updated_time', $mod );
  645. }
  646. return true;
  647. }
  648. /**
  649. * Outputs the Facebook app_id.
  650. *
  651. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  652. *
  653. * @return void
  654. */
  655. public function app_id() {
  656. $app_id = WPSEO_Options::get( 'fbadminapp', '' );
  657. if ( $app_id !== '' ) {
  658. $this->og_tag( 'fb:app_id', $app_id );
  659. }
  660. }
  661. /**
  662. * Outputs the site owner.
  663. *
  664. * @link https://developers.facebook.com/docs/reference/opengraph/object-type/article/
  665. * @return void
  666. *
  667. * @deprecated 7.1
  668. * @codeCoverageIgnore
  669. */
  670. public function site_owner() {
  671. // As this is a frontend method, we want to make sure it is not displayed for non-logged in users.
  672. if ( function_exists( 'wp_get_current_user' ) && current_user_can( 'manage_options' ) ) {
  673. _deprecated_function( 'WPSEO_OpenGraph::site_owner', '7.1', null );
  674. }
  675. }
  676. /**
  677. * Fallback method for plugins using image_output.
  678. *
  679. * @param string|bool $image Image URL.
  680. *
  681. * @deprecated 7.4
  682. * @codeCoverageIgnore
  683. */
  684. public function image_output( $image = false ) {
  685. _deprecated_function( 'WPSEO_OpenGraph::image_output', '7.4', 'WPSEO_OpenGraph::image' );
  686. $this->image( $image );
  687. }
  688. } /* End of class */