PageRenderTime 50ms CodeModel.GetById 14ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/wordpress-seo/frontend/class-twitter.php

https://bitbucket.org/carloskikea/helpet
PHP | 653 lines | 309 code | 102 blank | 242 comment | 77 complexity | 5b5c8581e4ca6ef12c748fe565b69d64 MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Frontend
  6 */
  7
  8/**
  9 * This class handles the Twitter card functionality.
 10 *
 11 * @link https://dev.twitter.com/docs/cards
 12 */
 13class WPSEO_Twitter {
 14
 15	/**
 16	 * @var    object    Instance of this class
 17	 */
 18	public static $instance;
 19
 20	/**
 21	 * @var array Images
 22	 */
 23	private $images = array();
 24
 25	/**
 26	 * @var array Images
 27	 */
 28	public $shown_images = array();
 29
 30	/** @var WPSEO_Frontend_Page_Type */
 31	protected $frontend_page_type;
 32
 33	/**
 34	 * Will hold the Twitter card type being created
 35	 *
 36	 * @var string
 37	 */
 38	private $type;
 39
 40	/**
 41	 * Class constructor
 42	 */
 43	public function __construct() {
 44		// Class for determine the current page type.
 45		$this->frontend_page_type = new WPSEO_Frontend_Page_Type();
 46
 47		$this->twitter();
 48	}
 49
 50	/**
 51	 * Outputs the Twitter Card code on singular pages.
 52	 */
 53	public function twitter() {
 54
 55		/**
 56		 * Filter: 'wpseo_output_twitter_card' - Allow disabling of the Twitter card
 57		 *
 58		 * @api bool $enabled Enabled/disabled flag
 59		 */
 60		if ( false === apply_filters( 'wpseo_output_twitter_card', true ) ) {
 61			return;
 62		}
 63
 64		wp_reset_query();
 65
 66		$this->type();
 67		$this->description();
 68		$this->title();
 69		$this->site_twitter();
 70		$this->image();
 71		if ( is_singular() ) {
 72			$this->author();
 73		}
 74
 75		/**
 76		 * Action: 'wpseo_twitter' - Hook to add all Yoast SEO Twitter output to so they're close together.
 77		 */
 78		do_action( 'wpseo_twitter' );
 79	}
 80
 81	/**
 82	 * Display the Twitter card type.
 83	 *
 84	 * This defaults to summary but can be filtered using the <code>wpseo_twitter_card_type</code> filter.
 85	 *
 86	 * @link https://dev.twitter.com/docs/cards
 87	 */
 88	protected function type() {
 89		$this->determine_card_type();
 90		$this->sanitize_card_type();
 91
 92		$this->output_metatag( 'card', $this->type );
 93	}
 94
 95	/**
 96	 * Determines the twitter card type for the current page
 97	 */
 98	private function determine_card_type() {
 99		$this->type = WPSEO_Options::get( 'twitter_card_type' );
100
101		// @todo This should be reworked to use summary_large_image for any fitting image R.
102		if ( is_singular() && has_shortcode( $GLOBALS['post']->post_content, 'gallery' ) ) {
103
104			$this->images = get_post_gallery_images();
105
106			if ( count( $this->images ) > 0 ) {
107				$this->type = 'summary_large_image';
108			}
109		}
110
111		/**
112		 * Filter: 'wpseo_twitter_card_type' - Allow changing the Twitter Card type as output in the Twitter card by Yoast SEO
113		 *
114		 * @api string $unsigned The type string
115		 */
116		$this->type = apply_filters( 'wpseo_twitter_card_type', $this->type );
117	}
118
119	/**
120	 * Determines whether the card type is of a type currently allowed by Twitter
121	 *
122	 * @link https://dev.twitter.com/cards/types
123	 */
124	private function sanitize_card_type() {
125		if ( ! in_array( $this->type, array(
126			'summary',
127			'summary_large_image',
128			'app',
129			'player',
130		), true )
131		) {
132			$this->type = 'summary';
133		}
134	}
135
136	/**
137	 * Output the metatag
138	 *
139	 * @param string $name    Tag name string.
140	 * @param string $value   Tag value string.
141	 * @param bool   $escaped Force escape flag.
142	 */
143	private function output_metatag( $name, $value, $escaped = false ) {
144
145		// Escape the value if not escaped.
146		if ( false === $escaped ) {
147			$value = esc_attr( $value );
148		}
149
150		/**
151		 * Filter: 'wpseo_twitter_metatag_key' - Make the Twitter metatag key filterable
152		 *
153		 * @api string $key The Twitter metatag key
154		 */
155		$metatag_key = apply_filters( 'wpseo_twitter_metatag_key', 'name' );
156
157		// Output meta.
158		echo '<meta ', esc_attr( $metatag_key ), '="twitter:', esc_attr( $name ), '" content="', $value, '" />', "\n";
159	}
160
161	/**
162	 * Displays the description for Twitter.
163	 *
164	 * Only used when OpenGraph is inactive.
165	 */
166	protected function description() {
167		if ( $this->frontend_page_type->is_simple_page() ) {
168			$meta_desc = $this->single_description( $this->frontend_page_type->get_simple_page_id() );
169		}
170		elseif ( is_category() || is_tax() || is_tag() ) {
171			$meta_desc = $this->taxonomy_description();
172		}
173		else {
174			$meta_desc = $this->fallback_description();
175		}
176
177		/**
178		 * Filter: 'wpseo_twitter_description' - Allow changing the Twitter description as output in the Twitter card by Yoast SEO
179		 *
180		 * @api string $twitter The description string
181		 */
182		$meta_desc = apply_filters( 'wpseo_twitter_description', $meta_desc );
183		if ( is_string( $meta_desc ) && $meta_desc !== '' ) {
184			$this->output_metatag( 'description', $meta_desc );
185		}
186	}
187
188	/**
189	 * Returns the description for a singular page
190	 *
191	 * @param int $post_id Post ID.
192	 *
193	 * @return string
194	 */
195	private function single_description( $post_id = 0 ) {
196		$meta_desc = trim( WPSEO_Meta::get_value( 'twitter-description', $post_id ) );
197
198		if ( is_string( $meta_desc ) && '' !== $meta_desc ) {
199			return $meta_desc;
200		}
201
202		$meta_desc = $this->fallback_description();
203		if ( is_string( $meta_desc ) && '' !== $meta_desc ) {
204			return $meta_desc;
205		}
206
207		return wp_strip_all_tags( get_the_excerpt() );
208	}
209
210
211	/**
212	 * Getting the description for the taxonomy
213	 *
214	 * @return bool|mixed|string
215	 */
216	private function taxonomy_description() {
217		$meta_desc = WPSEO_Taxonomy_Meta::get_meta_without_term( 'twitter-description' );
218
219		if ( ! is_string( $meta_desc ) || $meta_desc === '' ) {
220			$meta_desc = $this->fallback_description();
221		}
222
223		if ( is_string( $meta_desc ) || $meta_desc !== '' ) {
224			return $meta_desc;
225		}
226
227		return wp_strip_all_tags( term_description() );
228
229	}
230
231	/**
232	 * Returns a fallback description
233	 *
234	 * @return string
235	 */
236	private function fallback_description() {
237		return trim( WPSEO_Frontend::get_instance()->metadesc( false ) );
238	}
239
240	/**
241	 * Displays the title for Twitter.
242	 *
243	 * Only used when OpenGraph is inactive.
244	 */
245	protected function title() {
246		if ( $this->frontend_page_type->is_simple_page() ) {
247			$title = $this->single_title( $this->frontend_page_type->get_simple_page_id() );
248		}
249		elseif ( is_category() || is_tax() || is_tag() ) {
250			$title = $this->taxonomy_title();
251		}
252		else {
253			$title = $this->fallback_title();
254		}
255
256		/**
257		 * Filter: 'wpseo_twitter_title' - Allow changing the Twitter title as output in the Twitter card by Yoast SEO
258		 *
259		 * @api string $twitter The title string
260		 */
261		$title = apply_filters( 'wpseo_twitter_title', $title );
262		if ( is_string( $title ) && $title !== '' ) {
263			$this->output_metatag( 'title', $title );
264		}
265	}
266
267	/**
268	 * Returns the Twitter title for a single post
269	 *
270	 * @param int $post_id Post ID.
271	 *
272	 * @return string
273	 */
274	private function single_title( $post_id = 0 ) {
275		$title = WPSEO_Meta::get_value( 'twitter-title', $post_id );
276		if ( ! is_string( $title ) || $title === '' ) {
277			return $this->fallback_title();
278		}
279
280		return $title;
281	}
282
283	/**
284	 * Getting the title for the taxonomy
285	 *
286	 * @return bool|mixed|string
287	 */
288	private function taxonomy_title() {
289		$title = WPSEO_Taxonomy_Meta::get_meta_without_term( 'twitter-title' );
290
291		if ( ! is_string( $title ) || $title === '' ) {
292			return $this->fallback_title();
293		}
294
295		return $title;
296	}
297
298	/**
299	 * Returns the Twitter title for any page
300	 *
301	 * @return string
302	 */
303	private function fallback_title() {
304		return WPSEO_Frontend::get_instance()->title( '' );
305	}
306
307	/**
308	 * Displays the Twitter account for the site.
309	 */
310	protected function site_twitter() {
311		/**
312		 * Filter: 'wpseo_twitter_site' - Allow changing the Twitter site account as output in the Twitter card by Yoast SEO
313		 *
314		 * @api string $unsigned Twitter site account string
315		 */
316		$site = apply_filters( 'wpseo_twitter_site', WPSEO_Options::get( 'twitter_site' ) );
317		$site = $this->get_twitter_id( $site );
318
319		if ( is_string( $site ) && $site !== '' ) {
320			$this->output_metatag( 'site', '@' . $site );
321		}
322	}
323
324	/**
325	 * Checks if the given id is actually an id or a url and if url, distills the id from it.
326	 *
327	 * Solves issues with filters returning urls and theme's/other plugins also adding a user meta
328	 * twitter field which expects url rather than an id (which is what we expect).
329	 *
330	 * @param  string $id Twitter ID or url.
331	 *
332	 * @return string|bool Twitter ID or false if it failed to get a valid Twitter ID.
333	 */
334	private function get_twitter_id( $id ) {
335		if ( preg_match( '`([A-Za-z0-9_]{1,25})$`', $id, $match ) ) {
336			return $match[1];
337		}
338
339		return false;
340	}
341
342	/**
343	 * Displays the image for Twitter
344	 *
345	 * Only used when OpenGraph is inactive or Summary Large Image card is chosen.
346	 */
347	protected function image() {
348		if ( is_category() || is_tax() || is_tag() ) {
349			$this->taxonomy_image_output();
350		}
351		else {
352			$this->single_image_output();
353		}
354
355		if ( count( $this->shown_images ) === 0 && WPSEO_Options::get( 'og_default_image', '' ) !== '' ) {
356			$this->image_output( WPSEO_Options::get( 'og_default_image' ) );
357		}
358	}
359
360	/**
361	 * Outputs the first image of a gallery.
362	 */
363	private function gallery_images_output() {
364
365		$this->image_output( reset( $this->images ) );
366	}
367
368	/**
369	 * @return bool
370	 */
371	private function taxonomy_image_output() {
372		foreach ( array( 'twitter-image', 'opengraph-image' ) as $tag ) {
373			$img = WPSEO_Taxonomy_Meta::get_meta_without_term( $tag );
374			if ( is_string( $img ) && $img !== '' ) {
375				$this->image_output( $img );
376
377				return true;
378			}
379		}
380
381		/**
382		 * Filter: wpseo_twitter_taxonomy_image - Allow developers to set a custom Twitter image for taxonomies.
383		 *
384		 * @api bool|string $unsigned Return string to supply image to use, false to use no image.
385		 */
386		$img = apply_filters( 'wpseo_twitter_taxonomy_image', false );
387		if ( is_string( $img ) && $img !== '' ) {
388			$this->image_output( $img );
389
390			return true;
391		}
392
393		return false;
394	}
395
396	/**
397	 * Takes care of image output when we only need to display a single image.
398	 */
399	private function single_image_output() {
400		if ( $this->homepage_image_output() ) {
401			return;
402		}
403
404		if ( $this->posts_page_image_output() ) { // Posts page, which won't be caught by is_singular() below.
405			return;
406		}
407
408		if ( $this->frontend_page_type->is_simple_page() ) {
409			$post_id = $this->frontend_page_type->get_simple_page_id();
410
411			if ( $this->image_from_meta_values_output( $post_id ) ) {
412				return;
413			}
414
415			$post_id = get_the_ID();
416
417			if ( $this->image_of_attachment_page_output( $post_id ) ) {
418				return;
419			}
420			if ( $this->image_thumbnail_output( $post_id ) ) {
421				return;
422			}
423			if ( count( $this->images ) > 0 ) {
424				$this->gallery_images_output();
425				return;
426			}
427			if ( $this->image_from_content_output( $post_id ) ) {
428				return;
429			}
430		}
431	}
432
433	/**
434	 * Show the front page image
435	 *
436	 * @return bool
437	 */
438	private function homepage_image_output() {
439		if ( is_front_page() ) {
440			if ( WPSEO_Options::get( 'og_frontpage_image', '' ) !== '' ) {
441				$this->image_output( WPSEO_Options::get( 'og_frontpage_image' ) );
442
443				return true;
444			}
445		}
446
447		return false;
448	}
449
450	/**
451	 * Show the posts page image.
452	 *
453	 * @return bool
454	 */
455	private function posts_page_image_output() {
456
457		if ( is_front_page() || ! is_home() ) {
458			return false;
459		}
460
461		$post_id = get_option( 'page_for_posts' );
462
463		if ( $this->image_from_meta_values_output( $post_id ) ) {
464			return true;
465		}
466
467		if ( $this->image_thumbnail_output( $post_id ) ) {
468			return true;
469		}
470
471		return false;
472	}
473
474	/**
475	 * Outputs a Twitter image tag for a given image
476	 *
477	 * @param string  $img The source URL to the image.
478	 * @param boolean $tag Deprecated argument, previously used for gallery images.
479	 *
480	 * @return bool
481	 */
482	protected function image_output( $img, $tag = false ) {
483
484		if ( $tag ) {
485			_deprecated_argument( __METHOD__, 'WPSEO 2.4' );
486		}
487
488		/**
489		 * Filter: 'wpseo_twitter_image' - Allow changing the Twitter Card image
490		 *
491		 * @api string $img Image URL string
492		 */
493		$img = apply_filters( 'wpseo_twitter_image', $img );
494
495		if ( WPSEO_Utils::is_url_relative( $img ) === true && $img[0] === '/' ) {
496			$parsed_url = wp_parse_url( home_url() );
497			$img        = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $img;
498		}
499
500		$escaped_img = esc_url( $img );
501
502		if ( in_array( $escaped_img, $this->shown_images, true ) ) {
503			return false;
504		}
505
506		if ( is_string( $escaped_img ) && $escaped_img !== '' ) {
507			$this->output_metatag( 'image', $escaped_img, true );
508			array_push( $this->shown_images, $escaped_img );
509
510			return true;
511		}
512
513		return false;
514	}
515
516	/**
517	 * Retrieve images from the post meta values
518	 *
519	 * @param int $post_id Optional post ID to use.
520	 *
521	 * @return bool
522	 */
523	private function image_from_meta_values_output( $post_id = 0 ) {
524		foreach ( array( 'twitter-image', 'opengraph-image' ) as $tag ) {
525			$img = WPSEO_Meta::get_value( $tag, $post_id );
526			if ( $img !== '' ) {
527				$this->image_output( $img );
528
529				return true;
530			}
531		}
532
533		return false;
534	}
535
536	/**
537	 * Retrieve an attachment page's attachment
538	 *
539	 * @param string $attachment_id The ID of the attachment for which to retrieve the image.
540	 *
541	 * @return bool
542	 */
543	private function image_of_attachment_page_output( $attachment_id ) {
544		if ( get_post_type( $attachment_id ) === 'attachment' ) {
545			$mime_type = get_post_mime_type( $attachment_id );
546			switch ( $mime_type ) {
547				case 'image/jpeg':
548				case 'image/png':
549				case 'image/gif':
550					$this->image_output( wp_get_attachment_url( $attachment_id ) );
551					return true;
552			}
553		}
554
555		return false;
556	}
557
558	/**
559	 * Retrieve the featured image
560	 *
561	 * @param int $post_id Optional post ID to use.
562	 *
563	 * @return bool
564	 */
565	private function image_thumbnail_output( $post_id = 0 ) {
566
567		if ( empty( $post_id ) ) {
568			$post_id = get_the_ID();
569		}
570
571		if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $post_id ) ) {
572			/**
573			 * Filter: 'wpseo_twitter_image_size' - Allow changing the Twitter Card image size
574			 *
575			 * @api string $featured_img Image size string
576			 */
577			$featured_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), apply_filters( 'wpseo_twitter_image_size', 'full' ) );
578
579			if ( $featured_img ) {
580				$this->image_output( $featured_img[0] );
581
582				return true;
583			}
584		}
585
586		return false;
587	}
588
589	/**
590	 * Retrieve the image from the content
591	 *
592	 * @param int $post_id The post id to extract the images from.
593	 *
594	 * @return bool
595	 */
596	private function image_from_content_output( $post_id ) {
597		/**
598		 * Filter: 'wpseo_pre_analysis_post_content' - Allow filtering the content before analysis
599		 *
600		 * @api string $post_content The Post content string
601		 *
602		 * @param object $post - The post object.
603		 */
604		$post    = get_post( $post_id );
605		$content = apply_filters( 'wpseo_pre_analysis_post_content', $post->post_content, $post );
606
607		if ( preg_match_all( '`<img [^>]+>`', $content, $matches ) ) {
608			foreach ( $matches[0] as $img ) {
609				if ( preg_match( '`src=(["\'])(.*?)\1`', $img, $match ) ) {
610					$this->image_output( $match[2] );
611
612					return true;
613				}
614			}
615		}
616
617		return false;
618	}
619
620	/**
621	 * Displays the authors Twitter account.
622	 */
623	protected function author() {
624		$twitter = ltrim( trim( get_the_author_meta( 'twitter', get_post()->post_author ) ), '@' );
625		/**
626		 * Filter: 'wpseo_twitter_creator_account' - Allow changing the Twitter account as output in the Twitter card by Yoast SEO
627		 *
628		 * @api string $twitter The twitter account name string
629		 */
630		$twitter = apply_filters( 'wpseo_twitter_creator_account', $twitter );
631		$twitter = $this->get_twitter_id( $twitter );
632
633		if ( is_string( $twitter ) && $twitter !== '' ) {
634			$this->output_metatag( 'creator', '@' . $twitter );
635		}
636		elseif ( WPSEO_Options::get( 'twitter_site', '' ) !== '' && is_string( WPSEO_Options::get( 'twitter_site' ) ) ) {
637			$this->output_metatag( 'creator', '@' . WPSEO_Options::get( 'twitter_site' ) );
638		}
639	}
640
641	/**
642	 * Get the singleton instance of this class
643	 *
644	 * @return object
645	 */
646	public static function get_instance() {
647		if ( ! ( self::$instance instanceof self ) ) {
648			self::$instance = new self();
649		}
650
651		return self::$instance;
652	}
653} /* End of class */