PluginProbe
Sharing Image / 2.0.13
Sharing Image v2.0.13
3.10 trunk 2.0 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0 3.1 3.2 3.3 All 29 releases
sharing-image / classes / class-meta.php

class-meta.php in Sharing Image 2.0.13, at classes/class-meta.php

245 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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">' . PHP_EOL,
67 esc_url( $image )
68 );
69
70 printf(
71 '<meta property="og:image:width" content="%s">' . PHP_EOL,
72 esc_attr( $width )
73 );
74
75 printf(
76 '<meta property="og:image:height" content="%s">' . PHP_EOL,
77 esc_attr( $height )
78 );
79
80 /**
81 * Hide twitter meta tags.
82 *
83 * @since 2.0.12
84 *
85 * @param bool $hide_twitter_meta Set true to hide poster meta.
86 */
87 $hide_twitter_meta = apply_filters( 'sharing_image_hide_twitter_meta', false );
88
89 if ( $hide_twitter_meta ) {
90 return;
91 }
92
93 print(
94 '<meta name="twitter:card" content="summary_large_image">' . PHP_EOL
95 );
96
97 printf(
98 '<meta name="twitter:image" content="%s">' . PHP_EOL,
99 esc_url( $image )
100 );
101 }
102
103 /**
104 * Public method to get Sharing Image poster.
105 *
106 * @param int $object_id Optional. Post ID or Taxonomy term ID.
107 * @param string $object_type Optional. Requested meta type, can be singular or taxonomy.
108 * Default is determined by the type of the template in which it is called.
109
110 * @return string Url to poster.
111 */
112 public function get_poster( $object_id = null, $object_type = null ) {
113 $poster = $this->get_poster_src( $object_id, $object_type );
114
115 if ( ! isset( $poster[0] ) ) {
116 return null;
117 }
118
119 return $poster[0];
120 }
121
122 /**
123 * Public method to get Sharing Image poster url, width and height.
124 *
125 * @param int $object_id Optional. Post ID or Taxonomy term ID.
126 * @param string $object_type Optional. Requested meta type, can be singular or taxonomy.
127 * Default is determined by the type of the template in which it is called.
128
129 * @return array|false Poster image, width and height data or false if undefined.
130 */
131 public function get_poster_src( $object_id = null, $object_type = null ) {
132 $poster = $this->get_widget_poster_src( $object_id, $object_type );
133
134 if ( false === $poster ) {
135 $poster = $this->settings->get_default_poster_src();
136 }
137
138 /**
139 * Filters default template image data.
140 *
141 * @param array|false $image List of image data, or boolean false if no image is available.
142 * @param int $object_id Post ID or Taxonomy term ID.
143 */
144 return apply_filters( 'sharing_image_poster_src', $poster, $object_id );
145 }
146
147 /**
148 * Get post or term meta Sharing Image poster data.
149 *
150 * @param int $object_id Optional. Post ID or Taxonomy term ID.
151 * @param string $object_type Optional. Requested meta type, can be singular or taxonomy.
152 * Default is determined by the type of the template in which it is called.
153
154 * @return array|false Widget poster image, width and height data or false if undefined.
155 */
156 public function get_widget_poster_src( $object_id = null, $object_type = null ) {
157 $meta = array();
158
159 if ( 'singular' !== $object_type && 'taxonomy' !== $object_type ) {
160 $object_type = null;
161 }
162
163 if ( is_null( $object_type ) && is_singular() ) {
164 $object_type = 'singular';
165 }
166
167 if ( is_null( $object_type ) && ( is_category() || is_tag() || is_tax() ) ) {
168 $object_type = 'taxonomy';
169 }
170
171 if ( 'singular' === $object_type ) {
172 $meta = $this->get_singular_poster_meta( $object_id );
173 }
174
175 if ( 'taxonomy' === $object_type ) {
176 $meta = $this->get_taxonomy_poster_meta( $object_id );
177 }
178
179 $poster = false;
180
181 if ( is_array( $meta ) ) {
182 $meta = wp_array_slice_assoc( $meta, array( 'poster', 'width', 'height' ) );
183
184 if ( 3 === count( $meta ) ) {
185 $poster = array_values( $meta );
186 }
187 }
188
189 return $poster;
190 }
191
192 /**
193 * Get singular template poster meta.
194 *
195 * @param int $post_id Optional. Post ID.
196
197 * @return array|false Singular poster meta. False if undefined.
198 */
199 private function get_singular_poster_meta( $post_id = null ) {
200 $post = get_post( $post_id );
201
202 if ( isset( $post->ID ) ) {
203 $post_id = $post->ID;
204 }
205
206 $meta = get_post_meta( $post_id, Widget::WIDGET_META, true );
207
208 /**
209 * Filters singular template poster data.
210 *
211 * @param array|false $meta Singular poster meta.
212 * @param int $post_id Post ID.
213 */
214 return apply_filters( 'sharing_image_singular_poster_meta', $meta, $post_id );
215 }
216
217 /**
218 * Get taxonomy template poster data.
219 *
220 * @param int $term_id Optional. Term ID.
221
222 * @return array|false Taxonomy poster meta. False if undefined.
223 */
224 private function get_taxonomy_poster_meta( $term_id = null ) {
225 if ( ! $this->settings->is_premium_features() ) {
226 return false;
227 }
228
229 if ( null === $term_id ) {
230 $term_id = get_queried_object_id();
231 }
232
233 // Get term meta using Widget meta name const.
234 $meta = get_term_meta( $term_id, Widget::WIDGET_META, true );
235
236 /**
237 * Filters taxonomy template poster data.
238 *
239 * @param array $meta Term poster image meta.
240 * @param int $post_id Term ID.
241 */
242 return apply_filters( 'sharing_image_taxonomy_poster_meta', $meta, $term_id );
243 }
244 }
245