1 | <?php |
||
12 | class oEmbed { |
||
13 | public static $provider_url = ''; |
||
14 | |||
15 | /** |
||
16 | * Initialize. |
||
17 | * |
||
18 | * Register the oEmbed handler and the provider. |
||
19 | * Fire off the provider handler if detected. |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | public static function init() { |
||
35 | |||
36 | /** |
||
37 | * Output a response as a provider for an entry oEmbed URL. |
||
38 | * |
||
39 | * For now we only output the JSON format and don't care about the size (width, height). |
||
40 | * Our only current use-case is for it to provide output to the Add Media / From URL box |
||
41 | * in WordPress 4.8. |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | public static function render_provider_request() { |
||
71 | |||
72 | /** |
||
73 | * Output the embed HTML. |
||
74 | * |
||
75 | * @param array $matches The regex matches from the provided regex when calling wp_embed_register_handler() |
||
76 | * @param array $attr Embed attributes. |
||
77 | * @param string $url The original URL that was matched by the regex. |
||
78 | * @param array $rawattr The original unmodified attributes. |
||
79 | * |
||
80 | * @return string The embed HTML. |
||
81 | */ |
||
82 | 3 | public static function render( $matches, $attr, $url, $rawattr ) { |
|
103 | |||
104 | /** |
||
105 | * Parse oEmbed regex matches and return View and Entry. |
||
106 | * |
||
107 | * @param array $matches The regex matches. |
||
108 | * @param string $url The URL of the embed. |
||
109 | * |
||
110 | * @return array (\GV\View, \GV\Entry) |
||
111 | */ |
||
112 | 2 | private static function parse_matches( $matches, $url ) { |
|
146 | |||
147 | /** |
||
148 | * Display a nice placeholder in the admin for the entry. |
||
149 | * |
||
150 | * @param \GV\View $view The View. |
||
151 | * @param \GV\Entry $entry The Entry. |
||
152 | * |
||
153 | * @return string A placeholder, with Mr. Floaty :) |
||
154 | */ |
||
155 | private static function render_admin( $view, $entry ) { |
||
173 | |||
174 | /** |
||
175 | * Generate a warning to users when previewing oEmbed in the Add Media modal. |
||
176 | * |
||
177 | * @return string HTML notice |
||
178 | */ |
||
179 | private static function render_preview_notice() { |
||
185 | |||
186 | /** |
||
187 | * Render the entry as an oEmbed. |
||
188 | * |
||
189 | * @param \GV\View $view The View. |
||
190 | * @param \GV\Entry $entry The Entry. |
||
191 | * |
||
192 | * @return string The rendered oEmbed. |
||
193 | */ |
||
194 | 2 | private static function render_frontend( $view, $entry ) { |
|
195 | /** Private, pending, draft, etc. */ |
||
196 | 2 | $public_states = get_post_stati( array( 'public' => true ) ); |
|
197 | 2 | if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) { |
|
198 | 1 | gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $view->ID ) ); |
|
199 | 1 | return __( 'You are not allowed to view this content.', 'gravityview' ); |
|
200 | } |
||
201 | |||
202 | 2 | if ( $entry['status'] != 'active' ) { |
|
203 | 1 | gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) ); |
|
204 | 1 | return __( 'You are not allowed to view this content.', 'gravityview' ); |
|
205 | } |
||
206 | |||
207 | 2 | if ( $view->settings->get( 'show_only_approved' ) ) { |
|
208 | 1 | if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) ) ) { |
|
209 | 1 | gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) ); |
|
210 | 1 | return __( 'You are not allowed to view this content.', 'gravityview' ); |
|
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * When this is embedded inside a view we should not display the widgets. |
||
216 | */ |
||
217 | 2 | $request = gravityview()->request; |
|
218 | 2 | $is_reembedded = false; // Assume not embedded unless detected otherwise. |
|
219 | 2 | if ( in_array( get_class( $request ), array( 'GV\Frontend_Request', 'GV\Mock_Request' ) ) ) { |
|
220 | 2 | if ( ( $_view = $request->is_view() ) && $_view->ID !== $view->ID ) { |
|
221 | $is_reembedded = true; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Remove Widgets on a nested embedded View. |
||
227 | */ |
||
228 | 2 | if ( $is_reembedded ) { |
|
229 | $view->widgets = new \GV\Widget_Collection(); |
||
230 | } |
||
231 | |||
232 | /** Remove the back link. */ |
||
233 | 2 | add_filter( 'gravityview/template/links/back/url', '__return_false' ); |
|
234 | |||
235 | 2 | $renderer = new \GV\Entry_Renderer(); |
|
236 | 2 | $output = $renderer->render( $entry, $view, gravityview()->request ); |
|
237 | 2 | $output = sprintf( '<div class="gravityview-oembed gravityview-oembed-entry gravityview-oembed-entry-%d">%s</div>', $entry->ID, $output ); |
|
238 | |||
239 | 2 | remove_filter( 'gravityview/template/links/back/url', '__return_false' ); |
|
240 | |||
241 | 2 | return $output; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Generate the Regular expression that matches embedded entries. |
||
246 | * |
||
247 | * Generates different regex if using permalinks and if not using permalinks |
||
248 | * |
||
249 | * @return string Regex code |
||
250 | */ |
||
251 | private static function get_entry_regex() { |
||
274 | |||
275 | /** |
||
276 | * Internal oEmbed output, shortcircuit without proxying to the provider. |
||
277 | */ |
||
278 | public static function pre_oembed_result( $result, $url, $args ) { |
||
292 | } |
||
293 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.