Completed
Push — master ( 1fd6be...155036 )
by Zack
13s
created

GravityView_oEmbed   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 411
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 12.7%

Importance

Changes 0
Metric Value
dl 0
loc 411
ccs 16
cts 126
cp 0.127
rs 8.295
c 0
b 0
f 0
wmc 42
lcom 2
cbo 4

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A initialize() 0 9 3
A getInstance() 0 10 2
A register_handler() 0 5 1
A add_provider() 0 3 1
B render_provider_request() 0 36 4
B get_handler_regex() 0 29 2
B get_postid_from_url_and_slug() 0 23 4
A get_entry_id() 0 3 1
B render_handler() 0 36 6
A generate_preview_notice() 0 6 1
C set_vars() 0 38 11
A render_admin() 0 20 1
A generate_entry_output() 0 21 1
A set_single_entry_id() 0 4 1
B render_frontend() 0 32 2

How to fix   Complexity   

Complex Class

Complex classes like GravityView_oEmbed often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GravityView_oEmbed, and based on these observations, apply Extract Interface, too.

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 14.

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.

Loading history...
2
/**
3
 * GravityView oEmbed handling
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    Katz Web Services, Inc.
8
 * @link      http://gravityview.co
9
 * @copyright Copyright 2014, Katz Web Services, Inc.
10
 * @since 1.6
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	die;
15
}
16
17
/**
18
 * Register oEmbed handlers for embedding GravityView data and render that data
19
 *
20
 * @since 1.6
21
 */
22
class GravityView_oEmbed {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
23
24
	protected $output = array();
25
	protected $entry_id = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
26
	protected $view_id = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
27
	protected $is_full_oembed_preview = false;
28
29
	static $instance = NULL;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $instance.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
30
31
	private function __construct() {}
32
33
	private function initialize() {
34
35
		add_action( 'init', array( $this, 'register_handler' ) );
36
		add_action( 'init', array( $this, 'add_provider' ) );
37
38
		if ( ! empty( $_GET['gv_oembed_provider'] ) && ! empty( $_GET['url'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
39
			add_action( 'template_redirect', array( $this, 'render_provider_request' ) );
40
		}
41
	}
42
43
	/**
44
	 * @return GravityView_oEmbed
45
	 * @since 1.6
46
	 */
47
	static function getInstance() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
The function name getInstance is in camel caps, but expected get_instance instead as per the coding standard.
Loading history...
48
49
		if( empty( self::$instance ) ) {
50
			self::$instance = new self;
51
52
			self::$instance->initialize();
53
		}
54
55
		return self::$instance;
56
	}
57
58
	/**
59
	 * Register the oEmbed handler
60
	 *
61
	 * @since 1.6
62
	 * @uses get_handler_regex
63
	 */
64
	function register_handler() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
66
		wp_embed_register_handler( 'gravityview_entry', $this->get_handler_regex(), array( $this, 'render_handler' ), 20000 );
67
68
	}
69
70
	/**
71
	 * Become an oEmbed provider for GravityView.
72
	 *
73
	 * @since 1.21.5.3
74
	 *
75
	 * @return void
76
	 */
77
	function add_provider() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
		wp_oembed_add_provider( $this->get_handler_regex(), add_query_arg( 'gv_oembed_provider', '1', site_url() ), true );
79
	}
80
81
	/**
82
	 * Output a response as a provider for an entry oEmbed URL.
83
	 *
84
	 * For now we only output the JSON format and don't care about the size (width, height).
85
	 * Our only current use-case is for it to provide output to the Add Media / From URL box
86
	 *  in WordPress 4.8.
87
	 *
88
	 * @since 1.21.5.3
89
	 *
90
	 * @return void
91
	 */
92
	function render_provider_request() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
		if ( ! empty( $_GET['url'] ) ) {
94
			$url = $_GET['url'];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
95
		} else {
96
			header( 'HTTP/1.0 404 Not Found' );
97
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method render_provider_request() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
98
		}
99
100
		preg_match( $this->get_handler_regex(), $url, $matches );
101
102
		// If not using permalinks, re-assign values for matching groups
103
		if ( ! empty( $matches['entry_slug2'] ) ) {
104
			$matches['is_cpt'] = $matches['is_cpt2'];
105
			$matches['slug'] = $matches['slug2'];
106
			$matches['entry_slug'] = $matches['entry_slug2'];
107
			unset( $matches['is_cpt2'], $matches['slug2'], $matches['entry_slug2'] );
108
		}
109
110
		// No Entry was found
111
		if ( empty( $matches['entry_slug'] ) ) {
112
			do_action('gravityview_log_error', 'GravityView_oEmbed[render_handler] $entry_slug not parsed by regex.', $matches );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
113
			header( 'HTTP/1.0 404 Not Found' );
114
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method render_provider_request() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
115
		}
116
117
		// Setup the data used
118
		$this->set_vars( $matches, null, $url, null );
119
120
		echo json_encode( array(
121
			'version' => '1.0',
122
			'provider_name' => 'gravityview',
123
			'provider_url' => add_query_arg( 'gv_oembed_provider', '1', site_url() ),
124
			'html' => $this->generate_preview_notice() . $this->render_frontend( null, null, null, null ),
125
		) );
126
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method render_provider_request() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
127
	}
128
129
	/**
130
	 * Generate the Regular expression that matches embedded entries.
131
	 *
132
	 * Generates different regex if using permalinks and if not using permalinks
133
	 *
134
	 * @since 1.6
135
	 *
136
	 * @return string Regex code
137
	 */
138
	private function get_handler_regex() {
139
140
		if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
141
			$entry_var_name = \GV\Entry::get_endpoint_name();
142
		} else {
143
			/** Deprecated. Use \GV\Entry::get_endpoint_name instead. */
144
			$entry_var_name = GravityView_Post_Types::get_entry_var_name();
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_Post_Types::get_entry_var_name() has been deprecated.

This method has been deprecated.

Loading history...
145
		}
146
147
		/**
148
		 * @filter `gravityview_slug` Modify the url part for a View. [Read the doc](http://docs.gravityview.co/article/62-changing-the-view-slug)
149
		 * @param string $rewrite_slug The slug shown in the URL
150
		 */
151
		$rewrite_slug = apply_filters( 'gravityview_slug', 'view' );
152
153
		// Only support embeds for current site
154
		$prefix = trailingslashit( home_url() );
155
156
		// Using permalinks
157
		$using_permalinks = $prefix . "(?P<is_cpt>{$rewrite_slug})?/?(?P<slug>.+?)/{$entry_var_name}/(?P<entry_slug>.+?)/?\$";
158
159
		// Not using permalinks
160
		$not_using_permalinks = $prefix . "(?:index.php)?\?(?P<is_cpt2>[^=]+)=(?P<slug2>[^&]+)&entry=(?P<entry_slug2>[^&]+)\$";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal (?:index.php)?\?(?P<is_c...?P<entry_slug2>[^&]+)\$ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
161
162
		// Catch either
163
		$match_regex = "(?:{$using_permalinks}|{$not_using_permalinks})";
164
165
		return '#'.$match_regex.'#i';
166
	}
167
168
	/**
169
	 * Get the post ID from an URL
170
	 *
171
	 * This is necessary because url_to_postid() doesn't work with permalinks off for custom post types
172
	 *
173
	 * @uses url_to_postid()
174
	 * @since 1.6
175
	 *
176
	 * @param string $url URL to get the post ID from
177
	 * @param string $slug The name of a post, used as backup way of checking for post ID
178
	 * @return int 0 if not found; int of URL post ID otherwise
179
	 */
180
	private function get_postid_from_url_and_slug( $url = '', $slug = '' ) {
181
182
		$post_id = url_to_postid( $url );
183
184
		$page_on_front = get_option( 'page_on_front' );
185
186
		if( (int) $post_id === (int) $page_on_front || empty( $post_id ) ) {
187
188
			$args = array(
189
				'post_status' => 'publish',
190
				'name' => $slug,
191
				'post_type' => array('any', 'gravityview'),
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
192
			);
193
194
			$posts = get_posts( $args );
195
196
			if( !empty( $posts ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
197
				$post_id = $posts[0]->ID;
198
			}
199
		}
200
201
		return $post_id;
202
	}
203
204
	/**
205
	 * Get the entry id for the current oEmbedded entry
206
	 *
207
	 * @since 1.6
208
	 *
209
	 * @return int|null
210
	 */
211
	public function get_entry_id() {
212
		return $this->entry_id;
213
	}
214
215
	/**
216
	 *
217
	 *
218
	 * @since 1.6
219
	 * @see GravityView_oEmbed::add_providers() for the regex
220
	 *
221
	 * @param array $matches The regex matches from the provided regex when calling wp_embed_register_handler()
222
	 * @param array $attr Embed attributes.
223
	 * @param string $url The original URL that was matched by the regex.
224
	 * @param array $rawattr The original unmodified attributes.
225
	 * @return string The embed HTML.
226
	 */
227
	public function render_handler( $matches, $attr, $url, $rawattr ) {
228
229
		// If not using permalinks, re-assign values for matching groups
230
		if( !empty( $matches['entry_slug2'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
231
			$matches['is_cpt'] = $matches['is_cpt2'];
232
			$matches['slug'] = $matches['slug2'];
233
			$matches['entry_slug'] = $matches['entry_slug2'];
234
			unset( $matches['is_cpt2'], $matches['slug2'], $matches['entry_slug2'] );
235
		}
236
237
		// No Entry was found
238
		if( empty( $matches['entry_slug'] ) ) {
239
240
			do_action('gravityview_log_error', 'GravityView_oEmbed[render_handler] $entry_slug not parsed by regex.', $matches );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
241
242
			return '';
243
		}
244
245
		$return = '';
246
247
		// Setup the data used
248
		$this->set_vars( $matches, $attr, $url, $rawattr );
249
250
		if( is_admin() && !$this->is_full_oembed_preview ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
251
			$return = $this->render_admin( $matches, $attr, $url, $rawattr );
252
		} else {
253
254
			if( $this->is_full_oembed_preview ) {
255
				$return .= $this->generate_preview_notice();
256
			}
257
258
			$return .= $this->render_frontend( $matches, $attr, $url, $rawattr );
259
		}
260
261
		return $return;
262
	}
263
264
265
	/**
266
	 * Generate a warning to users when previewing oEmbed in the Add Media modal
267
	 *
268
	 * @return string HTML notice
269
	 */
270
	private function generate_preview_notice() {
271
		$floaty = GravityView_Admin::get_floaty();
272
		$title = esc_html__( 'This will look better when it is embedded.', 'gravityview' );
273
		$message = esc_html__('Styles don\'t get loaded when being previewed, so the content below will look strange. Don\'t be concerned!', 'gravityview');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
274
		return '<div class="updated notice">'. $floaty. '<h3>'.$title.'</h3><p>'.$message.'</p><br style="clear:both;" /></div>';
275
	}
276
277
	/**
278
	 * Set entry_id and view_id from the data sent to render_handler
279
	 *
280
	 * @var $entry_id
281
	 * @var $view_id
282
	 *
283
	 * @see render_handler
284
	 */
285 1
	private function set_vars( $matches, $attr, $url, $rawattr ) {
0 ignored issues
show
Unused Code introduced by
The parameter $attr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $rawattr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
286
287 1
		$this->entry_id = $matches['entry_slug'];
288
289 1
		$post_id = $this->get_postid_from_url_and_slug( $url, $matches['slug'] );
290
291
		// The URL didn't have the View Custom Post Type structure.
292 1
		if( empty( $matches['is_cpt'] ) || $matches['is_cpt'] !== 'gravityview' ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
293
294 1
			do_action('gravityview_log_debug', 'GravityView_oEmbed[render_handler] Embedding an entry inside a post or page', $matches );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
295
296 1
			if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) && $post = get_post( $post_id ) ) {
297 1
				$views = \GV\View_Collection::from_post( $post );
298 1
				$views = $views->all();
299 1
				if ( ! empty( $views ) ) {
300
					/** maybe_get_view_id has a side-effect that adds retrieved views to the global scope */
301 1
					foreach ( $views as $view ) {
302 1
						if ( \GV\View::exists( $view->ID ) && ! gravityview()->views->contains( $view->ID ) ) {
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<GV\Core>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
303 1
							gravityview()->views->add( $view );
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<GV\Core>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
304
						}
305
					}
306
307 1
					$this->view_id = $views[0]->ID;
308
				}
309
			} else {
310
				/** Deprecated. */
311 1
				$this->view_id = GravityView_View_Data::getInstance()->maybe_get_view_id( $post_id );
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_View_Data::maybe_get_view_id() has been deprecated.

This method has been deprecated.

Loading history...
312
			}
313
314
		} else {
315
316
			$this->view_id = $post_id;
317
318
		}
319
320
		// The inline content has $_POST['type'] set to "embed", while the "Add Media" modal doesn't set that.
321 1
		$this->is_full_oembed_preview = ( isset( $_POST['action'] ) && $_POST['action'] === 'parse-embed' && !isset( $_POST['type'] ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
322 1
	}
323
324
	/**
325
	 * Display a nice placeholder in the admin for the entry
326
	 *
327
	 * @param array $matches The regex matches from the provided regex when calling wp_embed_register_handler()
328
	 * @param array $attr Embed attributes.
329
	 * @param string $url The original URL that was matched by the regex.
330
	 * @param array $rawattr The original unmodified attributes.
331
	 * @return string The embed HTML.
332
	 */
333
	private function render_admin( $matches, $attr, $url, $rawattr ) {
0 ignored issues
show
Unused Code introduced by
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $url is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $rawattr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
334
		global $wp_version;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
335
336
		// Floaty the astronaut
337
		$image = GravityView_Admin::get_floaty();
338
339
		$embed_heading = sprintf( esc_html__('Embed Entry %d', 'gravityview'), $this->entry_id );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
340
341
		$embed_text = sprintf( esc_html__('This entry will be displayed as it is configured in View %d', 'gravityview'), $this->view_id );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
342
343
		return '
344
		<div class="loading-placeholder" style="background-color:#e6f0f5;">
345
			<h3 style="margin:0; padding:0; font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Oxygen-Sans, Ubuntu, Cantarell, \'Helvetica Neue\', sans-serif;">'.$image.$embed_heading.'</h3>
346
			<p style="margin:0; padding:0; font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Oxygen-Sans, Ubuntu, Cantarell, \'Helvetica Neue\', sans-serif;">
347
				'.$embed_text.'
348
			</p>
349
			<br style="clear: both;">
350
		</div>';
351
352
	}
353
354
	private function generate_entry_output() {
355
356
		// Tell get_gravityview() to display a single entry
357
		add_filter( 'gravityview/is_single_entry', array( $this, 'set_single_entry_id' ) );
358
359
		ob_start();
360
361
		// Print the entry as configured in View
362
		the_gravityview( $this->view_id );
363
364
		$view_html = ob_get_clean();
365
366
		// Clean up the filter
367
		remove_filter( 'gravityview/is_single_entry', array( $this, 'set_single_entry_id' ) );
368
369
		// Strip the new lines that are generated--when editing an entry in particular, scripts are printed that
370
		// then are passed through wpautop() and everything looks terrible.
371
		$view_html = str_replace( "\n", ' ', $view_html );
372
373
		return $view_html;
374
	}
375
376
	/**
377
	 * Tell get_gravityview() to display a single entry
378
	 *
379
	 * REQUIRED FOR THE VIEW TO OUTPUT A SINGLE ENTRY
380
	 *
381
	 * @param bool|int $is_single_entry Existing single entry. False, because GV thinks we're in a post or page.
382
	 *
383
	 * @return int The current entry ID
384
	 */
385
	public function set_single_entry_id( $is_single_entry = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $is_single_entry is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
386
387
		return $this->entry_id;
388
	}
389
390
	/**
391
	 * GravityView embed entry handler
392
	 *
393
	 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
394
	 * @param array $attr Embed attributes.
395
	 * @param string $url The original URL that was matched by the regex.
396
	 * @param array $rawattr The original unmodified attributes.
397
	 * @return string The embed HTML.
398
	 */
399
	private function render_frontend( $matches, $attr, $url, $rawattr ) {
400
401
		// If it's already been parsed, don't re-output it.
402
		if( !empty( $this->output[ $this->entry_id ] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
403
			return $this->output[ $this->entry_id ];
404
		}
405
406
		$entry_output = $this->generate_entry_output();
407
408
		// Wrap a container div around the output to allow for custom styling
409
		$output = sprintf('<div class="gravityview-oembed gravityview-oembed-entry gravityview-oembed-entry-'.$this->entry_id.'">%s</div>', $entry_output );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
410
411
		/**
412
		 * @filter `gravityview/oembed/entry` Filter the output of the oEmbed entry embed
413
		 * @param string $output HTML of the embedded entry, with wrapper div
414
		 * @param GravityView_oEmbed $object The current GravityView_oEmbed instance
415
		 * @param array $atts Other passed parameters and info. \n
416
		 *  @var string $entry_output HTML of just the View output, without the wrapper \n
417
		 *  @var array  $matches Capture group matches from the regex \n
418
		 *  @var array $attr Embed attributes. \n
419
		 *  @var string $url The original URL that was matched by the regex. \n
420
		 *  @var array $rawattr The original unmodified attributes.
421
		 */
422
		$output = apply_filters('gravityview/oembed/entry', $output, $this, compact( $entry_output, $matches, $attr, $url, $rawattr ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
423
424
		unset( $entry_output );
425
426
		$this->output[ $this->entry_id ] = $output;
427
428
		return $this->output[ $this->entry_id ];
429
430
	}
431
432
}
433
434
GravityView_oEmbed::getInstance();