| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Meta class | |
| 3 | + * Display header meta. | |
| 4 | 4 | * |
| 5 | 5 | * @package sharing-image |
| 6 | 6 | * @author Anton Lukin |
| 7 | 7 | */ |
| @@ -12,50 +12,29 @@ | ||
| 12 | 12 | die; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Meta class | |
| 16 | + * Meta class. | |
| 17 | 17 | * |
| 18 | 18 | * @class Meta |
| 19 | 19 | */ |
| 20 | 20 | class Meta { |
| 21 | 21 | /** |
| 22 | - * The instance of Settings class. | |
| 23 | - * | |
| 24 | - * @var instance | |
| 25 | - */ | |
| 26 | - private $settings; | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * Widget constructor. | |
| 30 | - */ | |
| 31 | - public function __construct() { | |
| 32 | - $this->settings = new Settings(); | |
| 33 | - } | |
| 34 | - | |
| 35 | - /** | |
| 36 | 22 | * Init class actions and filters. |
| 37 | 23 | */ |
| 38 | - public function init() { | |
| 39 | - add_action( 'wp_head', array( $this, 'show_header' ) ); | |
| 24 | + public static function init() { | |
| 25 | + add_action( 'wp_head', array( __CLASS__, 'show_header' ) ); | |
| 40 | 26 | } |
| 41 | 27 | |
| 42 | 28 | /** |
| 43 | 29 | * Print poster image meta tags. |
| 44 | 30 | */ |
| 45 | - public function show_header() { | |
| 46 | - /** | |
| 47 | - * Easy way to hide poster meta. | |
| 48 | - * | |
| 49 | - * @param bool $hide_header Set true to hide poster meta. | |
| 50 | - */ | |
| 51 | - $hide_header = apply_filters( 'sharing_image_hide_meta', false ); | |
| 52 | - | |
| 53 | - if ( $hide_settings ) { | |
| 31 | + public static function show_header() { | |
| 32 | + if ( ! Config::is_custom_header_meta() ) { | |
| 54 | 33 | return; |
| 55 | 34 | } |
| 56 | 35 | |
| 57 | - $poster = $this->get_poster_src(); | |
| 36 | + $poster = self::get_poster_src(); | |
| 58 | 37 | |
| 59 | 38 | if ( false === $poster ) { |
| 60 | 39 | return; |
| 61 | 40 | } |
| @@ -62,32 +41,56 @@ | ||
| 62 | 41 | |
| 63 | 42 | list( $image, $width, $height ) = $poster; |
| 64 | 43 | |
| 65 | 44 | printf( |
| 66 | - '<meta property="og:image" content="%s">' . "\n", | |
| 45 | + '<meta property="og:image" content="%s">' . PHP_EOL, | |
| 67 | 46 | esc_url( $image ) |
| 68 | 47 | ); |
| 69 | 48 | |
| 70 | 49 | printf( |
| 71 | - '<meta property="og:image:width" content="%s">' . "\n", | |
| 50 | + '<meta property="og:image:width" content="%s">' . PHP_EOL, | |
| 72 | 51 | esc_attr( $width ) |
| 73 | 52 | ); |
| 74 | 53 | |
| 75 | 54 | printf( |
| 76 | - '<meta property="og:image:height" content="%s">' . "\n", | |
| 55 | + '<meta property="og:image:height" content="%s">' . PHP_EOL, | |
| 77 | 56 | esc_attr( $height ) |
| 78 | 57 | ); |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Hide twitter meta tags. | |
| 61 | + * | |
| 62 | + * @since 2.0.12 | |
| 63 | + * | |
| 64 | + * @param bool $hide_twitter_meta Set true to hide poster meta. | |
| 65 | + */ | |
| 66 | + $hide_twitter_meta = apply_filters( 'sharing_image_hide_twitter_meta', false ); | |
| 67 | + | |
| 68 | + if ( $hide_twitter_meta ) { | |
| 69 | + return; | |
| 70 | + } | |
| 71 | + | |
| 72 | + print( | |
| 73 | + '<meta name="twitter:card" content="summary_large_image">' . PHP_EOL | |
| 74 | + ); | |
| 75 | + | |
| 76 | + printf( | |
| 77 | + '<meta name="twitter:image" content="%s">' . PHP_EOL, | |
| 78 | + esc_url( $image ) | |
| 79 | + ); | |
| 79 | 80 | } |
| 80 | 81 | |
| 81 | 82 | /** |
| 82 | 83 | * Public method to get Sharing Image poster. |
| 83 | 84 | * |
| 84 | - * @param int $object_id Optional. Post ID or Taxonomy term ID. | |
| 85 | + * @param int $object_id Optional. Post ID or Taxonomy term ID. | |
| 86 | + * @param string $object_type Optional. Requested meta type, can be singular or taxonomy. | |
| 87 | + * Default is determined by the type of the template in which it is called. | |
| 85 | 88 | |
| 86 | - * @return string Url to poster. | |
| 89 | + * @return string|null Url to poster. | |
| 87 | 90 | */ |
| 88 | - public function get_poster( $object_id = null ) { | |
| 89 | - $poster = $this->get_poster_src( $object_id ); | |
| 91 | + public static function get_poster( $object_id = null, $object_type = null ) { | |
| 92 | + $poster = self::get_poster_src( $object_id, $object_type ); | |
| 90 | 93 | |
| 91 | 94 | if ( ! isset( $poster[0] ) ) { |
| 92 | 95 | return null; |
| 93 | 96 | } |
| @@ -97,17 +100,19 @@ | ||
| 97 | 100 | |
| 98 | 101 | /** |
| 99 | 102 | * Public method to get Sharing Image poster url, width and height. |
| 100 | 103 | * |
| 101 | - * @param int $object_id Optional. Post ID or Taxonomy term ID. | |
| 104 | + * @param int $object_id Optional. Post ID or Taxonomy term ID. | |
| 105 | + * @param string $object_type Optional. Requested meta type, can be singular or taxonomy. | |
| 106 | + * Default is determined by the type of the template in which it is called. | |
| 102 | 107 | |
| 103 | 108 | * @return array|false Poster image, width and height data or false if undefined. |
| 104 | 109 | */ |
| 105 | - public function get_poster_src( $object_id = null ) { | |
| 106 | - $poster = $this->get_widget_poster_src( $object_id ); | |
| 110 | + public static function get_poster_src( $object_id = null, $object_type = null ) { | |
| 111 | + $poster = self::get_widget_poster_src( $object_id, $object_type ); | |
| 107 | 112 | |
| 108 | 113 | if ( false === $poster ) { |
| 109 | - $poster = $this->settings->get_default_poster_src(); | |
| 114 | + $poster = Config::get_default_poster_src(); | |
| 110 | 115 | } |
| 111 | 116 | |
| 112 | 117 | /** |
| 113 | 118 | * Filters default template image data. |
| @@ -120,23 +125,37 @@ | ||
| 120 | 125 | |
| 121 | 126 | /** |
| 122 | 127 | * Get post or term meta Sharing Image poster data. |
| 123 | 128 | * |
| 124 | - * @param int $object_id Optional. Post ID or Taxonomy term ID. | |
| 129 | + * @param int $object_id Optional. Post ID or Taxonomy term ID. | |
| 130 | + * @param string $object_type Optional. Requested meta type, can be singular or taxonomy. | |
| 131 | + * Default is determined by the type of the template in which it is called. | |
| 125 | 132 | |
| 126 | 133 | * @return array|false Widget poster image, width and height data or false if undefined. |
| 127 | 134 | */ |
| 128 | - public function get_widget_poster_src( $object_id = null ) { | |
| 135 | + public static function get_widget_poster_src( $object_id = null, $object_type = null ) { | |
| 129 | 136 | $meta = array(); |
| 130 | 137 | |
| 131 | - if ( is_singular() ) { | |
| 132 | - $meta = $this->get_singular_poster_meta( $object_id ); | |
| 138 | + if ( 'singular' !== $object_type && 'taxonomy' !== $object_type ) { | |
| 139 | + $object_type = null; | |
| 133 | 140 | } |
| 134 | 141 | |
| 135 | - if ( is_category() || is_tag() || is_tax() ) { | |
| 136 | - $meta = $this->get_taxonomy_poster_meta( $object_id ); | |
| 142 | + if ( is_null( $object_type ) && is_singular() ) { | |
| 143 | + $object_type = 'singular'; | |
| 137 | 144 | } |
| 138 | 145 | |
| 146 | + if ( is_null( $object_type ) && ( is_category() || is_tag() || is_tax() ) ) { | |
| 147 | + $object_type = 'taxonomy'; | |
| 148 | + } | |
| 149 | + | |
| 150 | + if ( 'singular' === $object_type ) { | |
| 151 | + $meta = self::get_singular_poster_meta( $object_id ); | |
| 152 | + } | |
| 153 | + | |
| 154 | + if ( 'taxonomy' === $object_type ) { | |
| 155 | + $meta = self::get_taxonomy_poster_meta( $object_id ); | |
| 156 | + } | |
| 157 | + | |
| 139 | 158 | $poster = false; |
| 140 | 159 | |
| 141 | 160 | if ( is_array( $meta ) ) { |
| 142 | 161 | $meta = wp_array_slice_assoc( $meta, array( 'poster', 'width', 'height' ) ); |
| @@ -155,14 +174,17 @@ | ||
| 155 | 174 | * @param int $post_id Optional. Post ID. |
| 156 | 175 | |
| 157 | 176 | * @return array|false Singular poster meta. False if undefined. |
| 158 | 177 | */ |
| 159 | - private function get_singular_poster_meta( $post_id = null ) { | |
| 178 | + private static function get_singular_poster_meta( $post_id = null ) { | |
| 160 | 179 | $post = get_post( $post_id ); |
| 161 | 180 | |
| 162 | - // Get post meta using Widget meta name const. | |
| 163 | - $meta = get_post_meta( $post->ID, Widget::WIDGET_META, true ); | |
| 181 | + if ( isset( $post->ID ) ) { | |
| 182 | + $post_id = $post->ID; | |
| 183 | + } | |
| 164 | 184 | |
| 185 | + $meta = get_post_meta( $post_id, Widget::META_SOURCE, true ); | |
| 186 | + | |
| 165 | 187 | /** |
| 166 | 188 | * Filters singular template poster data. |
| 167 | 189 | * |
| 168 | 190 | * @param array|false $meta Singular poster meta. |
| @@ -177,19 +199,19 @@ | ||
| 177 | 199 | * @param int $term_id Optional. Term ID. |
| 178 | 200 | |
| 179 | 201 | * @return array|false Taxonomy poster meta. False if undefined. |
| 180 | 202 | */ |
| 181 | - private function get_taxonomy_poster_meta( $term_id = null ) { | |
| 182 | - if ( ! $this->settings->is_premium_features() ) { | |
| 203 | + private static function get_taxonomy_poster_meta( $term_id = null ) { | |
| 204 | + if ( ! Premium::is_premium_features() ) { | |
| 183 | 205 | return false; |
| 184 | 206 | } |
| 185 | 207 | |
| 186 | - if ( null === $term_id ) { | |
| 208 | + if ( is_null( $term_id ) ) { | |
| 187 | 209 | $term_id = get_queried_object_id(); |
| 188 | 210 | } |
| 189 | 211 | |
| 190 | 212 | // Get term meta using Widget meta name const. |
| 191 | - $meta = get_term_meta( $term_id, Widget::WIDGET_META, true ); | |
| 213 | + $meta = get_term_meta( $term_id, Widget::META_SOURCE, true ); | |
| 192 | 214 | |
| 193 | 215 | /** |
| 194 | 216 | * Filters taxonomy template poster data. |
| 195 | 217 | * |