Front_End
Class Front_End
Source
File: includes/libs/front-end.class.php
012 | class Front_End{ |
013 | /** |
014 | * @var Plugin |
015 | */ |
016 | private $plugin ; |
017 |
018 | /** |
019 | * The content embed filter priority |
020 | * |
021 | * @var int |
022 | */ |
023 | private $embed_filter_priority = 999; |
024 |
025 | /** |
026 | * Store a list of video post ID's to skip automatic embedding for. |
027 | * Used for video posts that have the block editor video position block |
028 | * or for posts that use the video position shortcode. |
029 | * |
030 | * @var array |
031 | */ |
032 | private $skip_autoembed = []; |
033 |
034 | /** |
035 | * Front_End constructor. |
036 | * @ignore |
037 | * |
038 | * @param Plugin $plugin |
039 | */ |
040 | public function __construct( Plugin $plugin ) { |
041 | $this ->plugin = $plugin ; |
042 |
043 | add_action( 'init' , [ $this , 'init' ], 9999 ); |
044 | } |
045 |
046 | /** |
047 | * Init action callback. |
048 | * Will set all filters and actions needed by the plugin to do embeds and perform front-end |
049 | * tasks. Used for internal purposes, should not be called manually. |
050 | */ |
051 | public function init(){ |
052 |
053 | $this ->embed_filter_priority = intval ( |
054 | /** |
055 | * Automatic video embedding in post content filter priority. |
056 | * |
057 | * @param int $priority The "the_content" filter priority used to automatically embed the video into the post content. |
058 | */ |
059 | apply_filters( |
060 | 'vimeotheque\embed_filter_priority' , |
061 | $this ->embed_filter_priority |
062 | ) |
063 | ); |
064 |
065 | // filter content to embed video |
066 | add_filter( 'the_content' , [ |
067 | $this , |
068 | 'embed_video' |
069 | ], $this ->embed_filter_priority, 1 ); |
070 |
071 | // add player script |
072 | add_action( 'wp_print_scripts' , [ |
073 | $this , |
074 | 'add_player_script' |
075 | ] ); |
076 |
077 | add_action( 'post_thumbnail_html' , [ |
078 | $this , |
079 | 'filter_thumbnail_html' |
080 | ], 10, 2 ); |
081 |
082 | /** |
083 | * Template function the_term() works by default only for post_tag taxonomy. |
084 | * This filter will add the plugin taxonomy for plugin custom post type |
085 | */ |
086 | // add this filter only in front-end |
087 | if ( ! is_admin() ){ |
088 | add_filter( 'get_the_terms' , [ |
089 | $this , |
090 | 'filter_video_terms' |
091 | ], 10, 3 ); |
092 | } |
093 | } |
094 |
095 | /** |
096 | * Post content filter callback to embed the video into the post content. |
097 | * The method is called on the "post_content" filter and embeds the attached video above |
098 | * or below the post content, depending on the setting from the plugin Settings or the individual post options. |
099 | * |
100 | * @param string $content The post content |
101 | * @return string The post content |
102 | */ |
103 | public function embed_video( $content ){ |
104 | if ( ! Helper::video_is_visible() ){ |
105 | return $content ; |
106 | } |
107 |
108 | global $post ; |
109 |
110 | $_post = get_post( $post ); |
111 |
112 | if ( ! $_post ){ |
113 | return $content ; |
114 | } |
115 |
116 | // check if post is password protected |
117 | if ( post_password_required( $_post ) ){ |
118 | return $content ; |
119 | } |
120 |
121 | // check if filters prevent auto embedding |
122 | if ( !Helper::is_autoembed_allowed() ){ |
123 | return $content ; |
124 | } |
125 |
126 | // if video is in skipped auto embed list (has block or the video position shortcode in content), don't embed |
127 | if ( $this ->skipped_autoembed( $_post ) ){ |
128 | return $content ; |
129 | } |
130 |
131 | $video_post = Helper::get_video_post( $_post ); |
132 | $settings = $video_post ->get_embed_options(); |
133 |
134 | if ( !in_array( $settings [ 'video_position' ], [ 'above-content' , 'below-content' ] ) ){ |
135 | return $content ; |
136 | } |
137 |
138 | $video_container = Helper::embed_video( $_post , [], false ); |
139 |
140 | // put the filter back for other posts; remove in method 'prevent_autoembeds' |
141 | add_filter( 'the_content' , [ |
142 | $GLOBALS [ 'wp_embed' ], |
143 | 'autoembed' |
144 | ], 8 ); |
145 |
146 | /** |
147 | * Fires before the video embed is placed into the post content. |
148 | * Action that runs when the video is set to be automatically inserted into the post content. |
149 | * |
150 | * @param Video_Post $video_post The \Vimeotheque\Video_Post object generated for the current post in loop. |
151 | */ |
152 | do_action( |
153 | 'vimeotheque\automatic_embed_in_content' , |
154 | $video_post |
155 | ); |
156 |
157 | if ( 'below-content' === $settings [ 'video_position' ] ){ |
158 | return $content . $video_container ; |
159 | } else { |
160 | return $video_container . $content ; |
161 | } |
162 | } |
163 |
164 | /** |
165 | * Post featured image filter callback. |
166 | * Filter for the WP featured image to replace it with the video embed if option is enabled from the plugin Settings. |
167 | * |
168 | * @param $html |
169 | * @param $post_id |
170 | * |
171 | * @return mixed|void |
172 | */ |
173 | public function filter_thumbnail_html( $html , $post_id ){ |
174 |
175 | if ( ! Helper::video_is_visible() ){ |
176 | return $html ; |
177 | } |
178 |
179 | $video = Helper::get_video_post( $post_id ); |
180 | if ( ! $video ->is_video() ){ |
181 | return $html ; |
182 | } |
183 |
184 | $options = $video ->get_embed_options(); |
185 | if ( 'replace-featured-image' !== $options [ 'video_position' ] ){ |
186 | return $html ; |
187 | } |
188 |
189 | $video_container = Helper::embed_video( $video , [], false ); |
190 |
191 | /** |
192 | * Filter the embed code when option to replace featured image is on. |
193 | * Filter is triggered when the option to embed videos in place of the featured image is activated. |
194 | * |
195 | * @param string $video_container The HTML element that will contrin the video embed. |
196 | * @param Video_Post $video The \Vimeotheque\Video_Post post object. |
197 | * @param string $thumbnail_html The featured image HTML code. |
198 | */ |
199 | return apply_filters( |
200 | 'vimeotheque_enhanced_embed\embed_code' , |
201 | $video_container , |
202 | $video , |
203 | $html |
204 | ); |
205 | } |
206 |
207 | /** |
208 | * Check if post should be skipped from autoembedding. |
209 | * |
210 | * @ignore |
211 | * |
212 | * @param \WP_Post $post |
213 | * |
214 | * @return bool |
215 | */ |
216 | private function skipped_autoembed( \WP_Post $post ){ |
217 | return in_array( $post ->ID, $this ->skip_autoembed ); |
218 | } |
219 |
220 | /** |
221 | * Embed player script on video pages. |
222 | * |
223 | * @ignore Internal functionality |
224 | * |
225 | * @return void |
226 | */ |
227 | public function add_player_script(){ |
228 | if ( ! Helper::video_is_visible() ){ |
229 | return ; |
230 | } |
231 | Helper::enqueue_player(); |
232 | } |
233 |
234 | /** |
235 | * Filter the tags for the custom post type implemented by this plugin. |
236 | * Useful in template pages using function the_tags() - this function works |
237 | * only for the default post_tag taxonomy; the filter adds functionality |
238 | * for plugin post type tag taxonomy |
239 | * |
240 | * @ignore Internal functionality |
241 | * |
242 | * @param array $terms - the terms found |
243 | * @param int $post_id - the id of the post |
244 | * @param string $taxonomy - the taxonomy searched for |
245 | * @return |
246 | * |
247 | */ |
248 | public function filter_video_terms( $terms , $post_id , $taxonomy ){ |
249 | // get the current post |
250 | $post = get_post( $post_id ); |
251 | if ( ! $post ){ |
252 | return $terms ; |
253 | } |
254 | // check only for the custom post type of the plugin |
255 | if ( $this ->plugin->get_cpt()->get_post_type() == $post ->post_type ){ |
256 | // the_tags() will check only for taxonomy post_tag. Check if this is what it's looking for and replace if true |
257 | if ( $taxonomy != $this ->plugin->get_cpt()->get_tag_tax() && 'post_tag' == $taxonomy ){ |
258 | return get_the_terms( $post_id , $this ->plugin->get_cpt()->get_tag_tax() ); |
259 | } |
260 | } |
261 | return $terms ; |
262 | } |
263 |
264 | /** |
265 | * Return the filter priority for the automaticembed in post content |
266 | * |
267 | * @return int |
268 | */ |
269 | public function get_embed_filter_priority(){ |
270 | return $this ->embed_filter_priority; |
271 | } |
272 |
273 | /** |
274 | * Remove filter set on post content to embed the video; |
275 | * prevents automatic video embed above or below content when called. |
276 | * |
277 | * @ignore Internal functionality |
278 | * |
279 | * @param int|false $post_id The post ID registered to skip the auto embedding for |
280 | */ |
281 | public function prevent_post_autoembed( $post_id = false ){ |
282 | if ( ! $post_id ){ |
283 | /** |
284 | * @var \WP_Post |
285 | */ |
286 | global $post ; |
287 | $post_id = $post ->ID; |
288 | } |
289 |
290 | $this ->skip_autoembed[ $post_id ] = $post_id ; |
291 | } |
292 | } |
Methods
- embed_video — Post content filter callback to embed the video into the post content.
- filter_thumbnail_html — Post featured image filter callback.
- get_embed_filter_priority — Return the filter priority for the automaticembed in post content
- init — Init action callback.