1 | <?php |
---|
2 | |
---|
3 | // dependencies |
---|
4 | require_once( FVP_DIR . 'php/class-main.php' ); |
---|
5 | |
---|
6 | /** |
---|
7 | * Class containing frontend functionality. |
---|
8 | * |
---|
9 | * Enqueue scripts/styles, replace featured images by featured videos or |
---|
10 | * insert the ajax request handlers, add 'has-post-video' class and |
---|
11 | * register the [featured-video-plus] shortcode. |
---|
12 | * |
---|
13 | * @since 1.0.0 |
---|
14 | */ |
---|
15 | class FVP_Frontend extends Featured_Video_Plus { |
---|
16 | |
---|
17 | /** |
---|
18 | * Creates a new instace of this class, saves the featured_video_instance. |
---|
19 | * |
---|
20 | * @since 1.0.0 |
---|
21 | */ |
---|
22 | public function __construct() { |
---|
23 | parent::__construct(); |
---|
24 | |
---|
25 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); |
---|
26 | |
---|
27 | add_filter( 'post_thumbnail_html', array( $this, 'filter_post_thumbnail' ), 99, 5 ); |
---|
28 | add_filter( 'post_class', array( $this, 'has_post_video_class' ) ); |
---|
29 | |
---|
30 | add_shortcode( 'featured-video-plus', array( $this, 'shortcode' ) ); |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * Enqueue all scripts and styles needed when viewing the frontend. |
---|
36 | * |
---|
37 | * @since 1.0.0 |
---|
38 | */ |
---|
39 | public function enqueue() { |
---|
40 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
---|
41 | |
---|
42 | $options = get_option( 'fvp-settings' ); |
---|
43 | $mode = ! empty( $options['mode'] ) ? $options['mode'] : null; |
---|
44 | |
---|
45 | wp_register_script( |
---|
46 | 'jquery.fitvids', |
---|
47 | FVP_URL . "js/jquery.fitvids$min.js", |
---|
48 | array( 'jquery' ), |
---|
49 | '1.1', |
---|
50 | false |
---|
51 | ); |
---|
52 | |
---|
53 | wp_register_script( |
---|
54 | 'jquery.domwindow', |
---|
55 | FVP_URL . "js/jquery.domwindow$min.js", |
---|
56 | array( 'jquery' ), |
---|
57 | FVP_VERSION |
---|
58 | ); |
---|
59 | |
---|
60 | // Basic dependencies. Is extended in the following. |
---|
61 | $jsdeps = array( 'jquery' ); |
---|
62 | $cssdeps = array(); |
---|
63 | |
---|
64 | // If the video loading is performed in a lazy fashion we cannot know onload |
---|
65 | // if there is a local (html5) video - we need to require mediaelement.js |
---|
66 | // just for the possibility that one will be loaded. |
---|
67 | if ( 'overlay' === $mode || 'dynamic' === $mode ) { |
---|
68 | $jsdeps[] = 'wp-mediaelement'; |
---|
69 | $cssdeps[] = 'wp-mediaelement'; |
---|
70 | } |
---|
71 | |
---|
72 | // Is responsive video functionality required? Only when width is set to |
---|
73 | // 'auto' and display mode is not set to overlay. |
---|
74 | if ( |
---|
75 | ! empty($options['sizing']['responsive']) && |
---|
76 | $options['sizing']['responsive'] |
---|
77 | ) { |
---|
78 | $jsdeps[] = 'jquery.fitvids'; |
---|
79 | } |
---|
80 | |
---|
81 | // Is modal functionality required? |
---|
82 | if ( 'overlay' === $mode ) { |
---|
83 | $jsdeps[] = 'jquery.domwindow'; |
---|
84 | } |
---|
85 | |
---|
86 | // general frontend script |
---|
87 | wp_enqueue_script( |
---|
88 | 'fvp-frontend', |
---|
89 | FVP_URL . "js/frontend$min.js", |
---|
90 | $jsdeps, |
---|
91 | FVP_VERSION |
---|
92 | ); |
---|
93 | |
---|
94 | // some context for JS |
---|
95 | wp_localize_script( 'fvp-frontend', 'fvpdata', array( |
---|
96 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
---|
97 | 'nonce' => wp_create_nonce( FVP_NAME . FVP_VERSION ), |
---|
98 | 'fitvids' => ! empty( $options['sizing']['responsive'] ) && |
---|
99 | $options['sizing']['responsive'], |
---|
100 | 'dynamic' => 'dynamic' === $mode, |
---|
101 | 'overlay' => 'overlay' === $mode, |
---|
102 | 'opacity' => 0.75, |
---|
103 | 'loadicon' => 'overlay' === $mode ? FVP_URL . 'img/loadicon_w.gif' : |
---|
104 | FVP_URL . 'img/loadicon_b.gif', |
---|
105 | 'playicon' => FVP_URL . 'img/playicon.png', |
---|
106 | 'width' => ! empty( $options['sizing']['width'] ) ? |
---|
107 | $options['sizing']['width'] : null |
---|
108 | )); |
---|
109 | |
---|
110 | // general frontend styles |
---|
111 | wp_enqueue_style( |
---|
112 | 'fvp-frontend', |
---|
113 | FVP_URL . 'styles/frontend.css', |
---|
114 | $cssdeps, |
---|
115 | FVP_VERSION |
---|
116 | ); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | /** |
---|
121 | * Display featured videos in place of featured images if a featured video is available and only if so desired by user. |
---|
122 | * |
---|
123 | * @see http://wordpress.stackexchange.com/a/41858 |
---|
124 | * @since 1.0.0 |
---|
125 | * |
---|
126 | * @param string $html featured image html, ready to echo |
---|
127 | * @param int $post_id id of target post |
---|
128 | * @param int $post_thumbnail_id id of featured image |
---|
129 | * @param string|array $size desired size of featured image / video |
---|
130 | * @param array $attr |
---|
131 | */ |
---|
132 | public function filter_post_thumbnail( |
---|
133 | $html, |
---|
134 | $post_id, |
---|
135 | $post_thumbnail_id, |
---|
136 | $size, |
---|
137 | $attr |
---|
138 | ) { |
---|
139 | $size = $this->get_size(); |
---|
140 | |
---|
141 | $options = get_option( 'fvp-settings' ); |
---|
142 | $mode = ! empty( $options['mode'] ) ? $options['mode'] : null; |
---|
143 | $conditions = ! empty( $options['conditions'] ) ? |
---|
144 | $options['conditions'] : null; |
---|
145 | $single_replace = is_single() && |
---|
146 | ! empty( $options['single_replace'] ) && $options['single_replace']; |
---|
147 | |
---|
148 | if ( ( 'manual' === $mode ) || |
---|
149 | ( ! self::check_conditions( $conditions ) ) || |
---|
150 | ( ! has_post_video( $post_id ) ) |
---|
151 | ) { |
---|
152 | return $html; |
---|
153 | |
---|
154 | } elseif ( 'dynamic' === $mode && ! $single_replace ) { |
---|
155 | return sprintf( |
---|
156 | '<a href="#" data-id="%1$s" class="fvp-dynamic post-thumbnail">%2$s</a>', |
---|
157 | $post_id, |
---|
158 | $html |
---|
159 | ); |
---|
160 | |
---|
161 | } elseif ( 'overlay' === $mode && ! $single_replace ) { |
---|
162 | return sprintf( |
---|
163 | '<a href="#" data-id="%1$s" class="fvp-overlay post-thumbnail">%2$s</a>' . |
---|
164 | '<div id="fvp-cache-%1$s" style="display: none;"></div>', |
---|
165 | $post_id, |
---|
166 | $html |
---|
167 | ); |
---|
168 | } |
---|
169 | |
---|
170 | return get_the_post_video( $post_id, $size ); |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | /** |
---|
175 | * Add a 'has-post-video' class to posts if appropriate. |
---|
176 | * |
---|
177 | * @since 2.0.0 |
---|
178 | * |
---|
179 | * @param {array} $classes Existing classes |
---|
180 | * @return {array} Updated classes |
---|
181 | */ |
---|
182 | public function has_post_video_class( $classes ) { |
---|
183 | global $post; |
---|
184 | |
---|
185 | if ( has_post_video( $post->ID ) ) { |
---|
186 | $classes[] = 'has-post-video'; |
---|
187 | } |
---|
188 | return $classes; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | /** |
---|
193 | * Shortcode for usage in post or page entries. Echos the post's featured video. |
---|
194 | * |
---|
195 | * @since 1.0.0 |
---|
196 | * |
---|
197 | * @param array $atts can contain the width and/or height how the featured video should be displayed in px, optional |
---|
198 | */ |
---|
199 | public function shortcode($atts){ |
---|
200 | $w = isset($atts['width']) ? $atts['width'] : ''; |
---|
201 | $h = isset($atts['height']) ? $atts['height'] : ''; |
---|
202 | |
---|
203 | if ( has_post_video() ) { |
---|
204 | return get_the_post_video( null, array( $w, $h ) ); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | /** |
---|
210 | * Check a given set of display conditions if one or more of them hold. If |
---|
211 | * an empty set is given, return true. |
---|
212 | * |
---|
213 | * @param {assoc} $conditions |
---|
214 | * @return {bool} |
---|
215 | */ |
---|
216 | private static function check_conditions( $conditions ) { |
---|
217 | if ( empty( $conditions ) ) { |
---|
218 | return true; |
---|
219 | } |
---|
220 | |
---|
221 | $conditions_hold = false; |
---|
222 | foreach ( $conditions AS $fun => $value ) { |
---|
223 | $negate = false; |
---|
224 | if ( '!' === $fun[0] ) { |
---|
225 | $negate = true; |
---|
226 | $fun = substr( $fun, 1 ); |
---|
227 | } |
---|
228 | |
---|
229 | if ( $value && function_exists( 'is_' . $fun ) ) { |
---|
230 | $call = call_user_func( 'is_' . $fun ); |
---|
231 | $conditions_hold = $conditions_hold || ( $negate ? ! $call : $call ); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | return $conditions_hold; |
---|
236 | } |
---|
237 | } |
---|