| 1 |
<?php |
| 2 |
/** |
| 3 |
* Display header meta. |
| 4 |
* |
| 5 |
* @package sharing-image |
| 6 |
* @author Anton Lukin |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Sharing_Image; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Meta class. |
| 17 |
* |
| 18 |
* @class Meta |
| 19 |
*/ |
| 20 |
class Meta { |
| 21 |
/** |
| 22 |
* Init class actions and filters. |
| 23 |
*/ |
| 24 |
public static function init() { |
| 25 |
add_action( 'wp_head', array( __CLASS__, 'show_header' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Print poster image meta tags. |
| 30 |
*/ |
| 31 |
public static function show_header() { |
| 32 |
if ( ! Config::is_custom_header_meta() ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
$poster = self::get_poster_src(); |
| 37 |
|
| 38 |
if ( false === $poster ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
list( $image, $width, $height ) = $poster; |
| 43 |
|
| 44 |
printf( |
| 45 |
'<meta property="og:image" content="%s">' . PHP_EOL, |
| 46 |
esc_url( $image ) |
| 47 |
); |
| 48 |
|
| 49 |
printf( |
| 50 |
'<meta property="og:image:width" content="%s">' . PHP_EOL, |
| 51 |
esc_attr( $width ) |
| 52 |
); |
| 53 |
|
| 54 |
printf( |
| 55 |
'<meta property="og:image:height" content="%s">' . PHP_EOL, |
| 56 |
esc_attr( $height ) |
| 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 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Public method to get Sharing Image poster. |
| 84 |
* |
| 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. |
| 88 |
|
| 89 |
* @return string|null Url to poster. |
| 90 |
*/ |
| 91 |
public static function get_poster( $object_id = null, $object_type = null ) { |
| 92 |
$poster = self::get_poster_src( $object_id, $object_type ); |
| 93 |
|
| 94 |
if ( ! isset( $poster[0] ) ) { |
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
return $poster[0]; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Public method to get Sharing Image poster url, width and height. |
| 103 |
* |
| 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. |
| 107 |
|
| 108 |
* @return array|false Poster image, width and height data or false if undefined. |
| 109 |
*/ |
| 110 |
public static function get_poster_src( $object_id = null, $object_type = null ) { |
| 111 |
$poster = self::get_widget_poster_src( $object_id, $object_type ); |
| 112 |
|
| 113 |
if ( false === $poster ) { |
| 114 |
$poster = Config::get_default_poster_src(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Filters default template image data. |
| 119 |
* |
| 120 |
* @param array|false $image List of image data, or boolean false if no image is available. |
| 121 |
* @param int $object_id Post ID or Taxonomy term ID. |
| 122 |
*/ |
| 123 |
return apply_filters( 'sharing_image_poster_src', $poster, $object_id ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get post or term meta Sharing Image poster data. |
| 128 |
* |
| 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. |
| 132 |
|
| 133 |
* @return array|false Widget poster image, width and height data or false if undefined. |
| 134 |
*/ |
| 135 |
public static function get_widget_poster_src( $object_id = null, $object_type = null ) { |
| 136 |
$meta = array(); |
| 137 |
|
| 138 |
if ( 'singular' !== $object_type && 'taxonomy' !== $object_type ) { |
| 139 |
$object_type = null; |
| 140 |
} |
| 141 |
|
| 142 |
if ( is_null( $object_type ) && is_singular() ) { |
| 143 |
$object_type = 'singular'; |
| 144 |
} |
| 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 |
|
| 158 |
$poster = false; |
| 159 |
|
| 160 |
if ( is_array( $meta ) ) { |
| 161 |
$meta = wp_array_slice_assoc( $meta, array( 'poster', 'width', 'height' ) ); |
| 162 |
|
| 163 |
if ( 3 === count( $meta ) ) { |
| 164 |
$poster = array_values( $meta ); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return $poster; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get singular template poster meta. |
| 173 |
* |
| 174 |
* @param int $post_id Optional. Post ID. |
| 175 |
|
| 176 |
* @return array|false Singular poster meta. False if undefined. |
| 177 |
*/ |
| 178 |
private static function get_singular_poster_meta( $post_id = null ) { |
| 179 |
$post = get_post( $post_id ); |
| 180 |
|
| 181 |
if ( isset( $post->ID ) ) { |
| 182 |
$post_id = $post->ID; |
| 183 |
} |
| 184 |
|
| 185 |
$meta = get_post_meta( $post_id, Widget::META_SOURCE, true ); |
| 186 |
|
| 187 |
/** |
| 188 |
* Filters singular template poster data. |
| 189 |
* |
| 190 |
* @param array|false $meta Singular poster meta. |
| 191 |
* @param int $post_id Post ID. |
| 192 |
*/ |
| 193 |
return apply_filters( 'sharing_image_singular_poster_meta', $meta, $post_id ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Get taxonomy template poster data. |
| 198 |
* |
| 199 |
* @param int $term_id Optional. Term ID. |
| 200 |
|
| 201 |
* @return array|false Taxonomy poster meta. False if undefined. |
| 202 |
*/ |
| 203 |
private static function get_taxonomy_poster_meta( $term_id = null ) { |
| 204 |
if ( ! Premium::is_premium_features() ) { |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
if ( is_null( $term_id ) ) { |
| 209 |
$term_id = get_queried_object_id(); |
| 210 |
} |
| 211 |
|
| 212 |
// Get term meta using Widget meta name const. |
| 213 |
$meta = get_term_meta( $term_id, Widget::META_SOURCE, true ); |
| 214 |
|
| 215 |
/** |
| 216 |
* Filters taxonomy template poster data. |
| 217 |
* |
| 218 |
* @param array $meta Term poster image meta. |
| 219 |
* @param int $post_id Term ID. |
| 220 |
*/ |
| 221 |
return apply_filters( 'sharing_image_taxonomy_poster_meta', $meta, $term_id ); |
| 222 |
} |
| 223 |
} |
| 224 |
|