| 1 |
<?php |
| 2 |
/** |
| 3 |
* Meta class |
| 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 |
* 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 |
* Init class actions and filters. |
| 37 |
*/ |
| 38 |
public function init() { |
| 39 |
add_action( 'wp_head', array( $this, 'show_header' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Print poster image meta tags. |
| 44 |
*/ |
| 45 |
public function show_header() { |
| 46 |
/** |
| 47 |
* Easy way to hide poster meta. |
| 48 |
* |
| 49 |
* @param bool $hide_meta Set true to hide poster meta. |
| 50 |
*/ |
| 51 |
$hide_meta = apply_filters( 'sharing_image_hide_meta', false ); |
| 52 |
|
| 53 |
if ( $hide_meta ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$poster = $this->get_poster_src(); |
| 58 |
|
| 59 |
if ( false === $poster ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
list( $image, $width, $height ) = $poster; |
| 64 |
|
| 65 |
printf( |
| 66 |
'<meta property="og:image" content="%s">' . "\n", |
| 67 |
esc_url( $image ) |
| 68 |
); |
| 69 |
|
| 70 |
printf( |
| 71 |
'<meta property="og:image:width" content="%s">' . "\n", |
| 72 |
esc_attr( $width ) |
| 73 |
); |
| 74 |
|
| 75 |
printf( |
| 76 |
'<meta property="og:image:height" content="%s">' . "\n", |
| 77 |
esc_attr( $height ) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Public method to get Sharing Image poster. |
| 83 |
* |
| 84 |
* @param int $object_id Optional. Post ID or Taxonomy term ID. |
| 85 |
|
| 86 |
* @return string Url to poster. |
| 87 |
*/ |
| 88 |
public function get_poster( $object_id = null ) { |
| 89 |
$poster = $this->get_poster_src( $object_id ); |
| 90 |
|
| 91 |
if ( ! isset( $poster[0] ) ) { |
| 92 |
return null; |
| 93 |
} |
| 94 |
|
| 95 |
return $poster[0]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Public method to get Sharing Image poster url, width and height. |
| 100 |
* |
| 101 |
* @param int $object_id Optional. Post ID or Taxonomy term ID. |
| 102 |
|
| 103 |
* @return array|false Poster image, width and height data or false if undefined. |
| 104 |
*/ |
| 105 |
public function get_poster_src( $object_id = null ) { |
| 106 |
$poster = $this->get_widget_poster_src( $object_id ); |
| 107 |
|
| 108 |
if ( false === $poster ) { |
| 109 |
$poster = $this->settings->get_default_poster_src(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Filters default template image data. |
| 114 |
* |
| 115 |
* @param array|false $image List of image data, or boolean false if no image is available. |
| 116 |
* @param int $object_id Post ID or Taxonomy term ID. |
| 117 |
*/ |
| 118 |
return apply_filters( 'sharing_image_poster_src', $poster, $object_id ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get post or term meta Sharing Image poster data. |
| 123 |
* |
| 124 |
* @param int $object_id Optional. Post ID or Taxonomy term ID. |
| 125 |
|
| 126 |
* @return array|false Widget poster image, width and height data or false if undefined. |
| 127 |
*/ |
| 128 |
public function get_widget_poster_src( $object_id = null ) { |
| 129 |
$meta = array(); |
| 130 |
|
| 131 |
if ( is_singular() ) { |
| 132 |
$meta = $this->get_singular_poster_meta( $object_id ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( is_category() || is_tag() || is_tax() ) { |
| 136 |
$meta = $this->get_taxonomy_poster_meta( $object_id ); |
| 137 |
} |
| 138 |
|
| 139 |
$poster = false; |
| 140 |
|
| 141 |
if ( is_array( $meta ) ) { |
| 142 |
$meta = wp_array_slice_assoc( $meta, array( 'poster', 'width', 'height' ) ); |
| 143 |
|
| 144 |
if ( 3 === count( $meta ) ) { |
| 145 |
$poster = array_values( $meta ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $poster; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get singular template poster meta. |
| 154 |
* |
| 155 |
* @param int $post_id Optional. Post ID. |
| 156 |
|
| 157 |
* @return array|false Singular poster meta. False if undefined. |
| 158 |
*/ |
| 159 |
private function get_singular_poster_meta( $post_id = null ) { |
| 160 |
$post = get_post( $post_id ); |
| 161 |
|
| 162 |
// Get post meta using Widget meta name const. |
| 163 |
$meta = get_post_meta( $post->ID, Widget::WIDGET_META, true ); |
| 164 |
|
| 165 |
/** |
| 166 |
* Filters singular template poster data. |
| 167 |
* |
| 168 |
* @param array|false $meta Singular poster meta. |
| 169 |
* @param int $post_id Post ID. |
| 170 |
*/ |
| 171 |
return apply_filters( 'sharing_image_singular_poster_meta', $meta, $post_id ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get taxonomy template poster data. |
| 176 |
* |
| 177 |
* @param int $term_id Optional. Term ID. |
| 178 |
|
| 179 |
* @return array|false Taxonomy poster meta. False if undefined. |
| 180 |
*/ |
| 181 |
private function get_taxonomy_poster_meta( $term_id = null ) { |
| 182 |
if ( ! $this->settings->is_premium_features() ) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
if ( null === $term_id ) { |
| 187 |
$term_id = get_queried_object_id(); |
| 188 |
} |
| 189 |
|
| 190 |
// Get term meta using Widget meta name const. |
| 191 |
$meta = get_term_meta( $term_id, Widget::WIDGET_META, true ); |
| 192 |
|
| 193 |
/** |
| 194 |
* Filters taxonomy template poster data. |
| 195 |
* |
| 196 |
* @param array $meta Term poster image meta. |
| 197 |
* @param int $post_id Term ID. |
| 198 |
*/ |
| 199 |
return apply_filters( 'sharing_image_taxonomy_poster_meta', $meta, $term_id ); |
| 200 |
} |
| 201 |
} |
| 202 |
|