Completed
Push — develop ( 56fa65...fcf08a )
by
unknown
11:12
created

GravityView_oEmbed   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 405
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 12.8%

Importance

Changes 0
Metric Value
dl 0
loc 405
ccs 16
cts 125
cp 0.128
rs 8.2769
c 0
b 0
f 0
wmc 41
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
A get_postid_from_url_and_slug() 0 21 3
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
	 * @return void
74
	 */
75
	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...
76
		wp_oembed_add_provider( $this->get_handler_regex(), add_query_arg( 'gv_oembed_provider', '1', site_url() ), true );
77
	}
78
79
	/**
80
	 * Output a response as a provider for an entry oEmbed URL.
81
	 *
82
	 * For now we only output the JSON format and don't care about the size (width, height).
83
	 * Our only current use-case is for it to provide output to the Add Media / From URL box
84
	 *  in WordPress 4.8.
85
	 *
86
	 * @return void
87
	 */
88
	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...
89
		if ( ! empty( $_GET['url'] ) ) {
90
			$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...
91
		} else {
92
			header( 'HTTP/1.0 404 Not Found' );
93
			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...
94
		}
95
96
		preg_match( $this->get_handler_regex(), $url, $matches );
97
98
		// If not using permalinks, re-assign values for matching groups
99
		if ( ! empty( $matches['entry_slug2'] ) ) {
100
			$matches['is_cpt'] = $matches['is_cpt2'];
101
			$matches['slug'] = $matches['slug2'];
102
			$matches['entry_slug'] = $matches['entry_slug2'];
103
			unset( $matches['is_cpt2'], $matches['slug2'], $matches['entry_slug2'] );
104
		}
105
106
		// No Entry was found
107
		if ( empty( $matches['entry_slug'] ) ) {
108
			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...
109
			header( 'HTTP/1.0 404 Not Found' );
110
			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...
111
		}
112
113
		// Setup the data used
114
		$this->set_vars( $matches, null, $url, null );
115
116
		echo json_encode( array(
117
			'version' => '1.0',
118
			'provider_name' => 'gravityview',
119
			'provider_url' => add_query_arg( 'gv_oembed_provider', '1', site_url() ),
120
			'html' => $this->generate_preview_notice() . $this->render_frontend( null, null, null, null ),
121
		) );
122
		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...
123
	}
124
125
	/**
126
	 * Generate the Regular expression that matches embedded entries.
127
	 *
128
	 * Generates different regex if using permalinks and if not using permalinks
129
	 *
130
	 * @since 1.6
131
	 *
132
	 * @return string Regex code
133
	 */
134
	private function get_handler_regex() {
135
136
		if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) {
137
			$entry_var_name = \GV\Entry::get_endpoint_name();
138
		} else {
139
			/** Deprecated. Use \GV\Entry::get_endpoint_name instead. */
140
			$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...
141
		}
142
143
		/**
144
		 * @filter `gravityview_slug` Modify the url part for a View. [Read the doc](http://docs.gravityview.co/article/62-changing-the-view-slug)
145
		 * @param string $rewrite_slug The slug shown in the URL
146
		 */
147
		$rewrite_slug = apply_filters( 'gravityview_slug', 'view' );
148
149
		// Only support embeds for current site
150
		$prefix = trailingslashit( home_url() );
151
152
		// Using permalinks
153
		$using_permalinks = $prefix . "(?P<is_cpt>{$rewrite_slug})?/?(?P<slug>.+?)/{$entry_var_name}/(?P<entry_slug>.+?)/?\$";
154
155
		// Not using permalinks
156
		$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...
157
158
		// Catch either
159
		$match_regex = "(?:{$using_permalinks}|{$not_using_permalinks})";
160
161
		return '#'.$match_regex.'#i';
162
	}
163
164
	/**
165
	 * Get the post ID from an URL
166
	 *
167
	 * This is necessary because url_to_postid() doesn't work with permalinks off for custom post types
168
	 *
169
	 * @uses url_to_postid()
170
	 * @since 1.6
171
	 *
172
	 * @param string $url URL to get the post ID from
173
	 * @param string $slug The name of a post, used as backup way of checking for post ID
174
	 * @return int 0 if not found; int of URL post ID otherwise
175
	 */
176
	private function get_postid_from_url_and_slug( $url = '', $slug = '' ) {
177
178
		$post_id = url_to_postid( $url );
179
180
		if( empty( $post_id ) ) {
181
182
			$args = array(
183
				'post_status' => 'publish',
184
				'name' => $slug,
185
				'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...
186
			);
187
188
			$posts = get_posts( $args );
189
190
			if( !empty( $posts ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
191
				$post_id = $posts[0]->ID;
192
			}
193
		}
194
195
		return $post_id;
196
	}
197
198
	/**
199
	 * Get the entry id for the current oEmbedded entry
200
	 *
201
	 * @since 1.6
202
	 *
203
	 * @return int|null
204
	 */
205
	public function get_entry_id() {
206
		return $this->entry_id;
207
	}
208
209
	/**
210
	 *
211
	 *
212
	 * @since 1.6
213
	 * @see GravityView_oEmbed::add_providers() for the regex
214
	 *
215
	 * @param array $matches The regex matches from the provided regex when calling wp_embed_register_handler()
216
	 * @param array $attr Embed attributes.
217
	 * @param string $url The original URL that was matched by the regex.
218
	 * @param array $rawattr The original unmodified attributes.
219
	 * @return string The embed HTML.
220
	 */
221
	public function render_handler( $matches, $attr, $url, $rawattr ) {
222
223
		// If not using permalinks, re-assign values for matching groups
224
		if( !empty( $matches['entry_slug2'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
225
			$matches['is_cpt'] = $matches['is_cpt2'];
226
			$matches['slug'] = $matches['slug2'];
227
			$matches['entry_slug'] = $matches['entry_slug2'];
228
			unset( $matches['is_cpt2'], $matches['slug2'], $matches['entry_slug2'] );
229
		}
230
231
		// No Entry was found
232
		if( empty( $matches['entry_slug'] ) ) {
233
234
			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...
235
236
			return '';
237
		}
238
239
		$return = '';
240
241
		// Setup the data used
242
		$this->set_vars( $matches, $attr, $url, $rawattr );
243
244
		if( is_admin() && !$this->is_full_oembed_preview ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
245
			$return = $this->render_admin( $matches, $attr, $url, $rawattr );
246
		} else {
247
248
			if( $this->is_full_oembed_preview ) {
249
				$return .= $this->generate_preview_notice();
250
			}
251
252
			$return .= $this->render_frontend( $matches, $attr, $url, $rawattr );
253
		}
254
255
		return $return;
256
	}
257
258
259
	/**
260
	 * Generate a warning to users when previewing oEmbed in the Add Media modal
261
	 *
262
	 * @return string HTML notice
263
	 */
264
	private function generate_preview_notice() {
265
		$floaty = GravityView_Admin::get_floaty();
266
		$title = esc_html__( 'This will look better when it is embedded.', 'gravityview' );
267
		$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...
268
		return '<div class="updated notice">'. $floaty. '<h3>'.$title.'</h3><p>'.$message.'</p><br style="clear:both;" /></div>';
269
	}
270
271
	/**
272
	 * Set entry_id and view_id from the data sent to render_handler
273
	 *
274
	 * @var $entry_id
275
	 * @var $view_id
276
	 *
277
	 * @see render_handler
278
	 */
279 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...
280
281 1
		$this->entry_id = $matches['entry_slug'];
282
283 1
		$post_id = $this->get_postid_from_url_and_slug( $url, $matches['slug'] );
284
285
		// The URL didn't have the View Custom Post Type structure.
286 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...
287
288 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...
289
290 1
			if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) && $post = get_post( $post_id ) ) {
291 1
				$views = \GV\View_Collection::from_post( $post );
292 1
				$views = $views->all();
293 1
				if ( ! empty( $views ) ) {
294
					/** maybe_get_view_id has a side-effect that adds retrieved views to the global scope */
295 1
					foreach ( $views as $view ) {
296 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...
297 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...
298
						}
299
					}
300
301 1
					$this->view_id = $views[0]->ID;
302
				}
303
			} else {
304
				/** Deprecated. */
305 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...
306
			}
307
308
		} else {
309
310
			$this->view_id = $post_id;
311
312
		}
313
314
		// The inline content has $_POST['type'] set to "embed", while the "Add Media" modal doesn't set that.
315 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...
316 1
	}
317
318
	/**
319
	 * Display a nice placeholder in the admin for the entry
320
	 *
321
	 * @param array $matches The regex matches from the provided regex when calling wp_embed_register_handler()
322
	 * @param array $attr Embed attributes.
323
	 * @param string $url The original URL that was matched by the regex.
324
	 * @param array $rawattr The original unmodified attributes.
325
	 * @return string The embed HTML.
326
	 */
327
	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...
328
		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...
329
330
		// Floaty the astronaut
331
		$image = GravityView_Admin::get_floaty();
332
333
		$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...
334
335
		$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...
336
337
		return '
338
		<div class="loading-placeholder" style="background-color:#e6f0f5;">
339
			<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>
340
			<p style="margin:0; padding:0; font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Oxygen-Sans, Ubuntu, Cantarell, \'Helvetica Neue\', sans-serif;">
341
				'.$embed_text.'
342
			</p>
343
			<br style="clear: both;">
344
		</div>';
345
346
	}
347
348
	private function generate_entry_output() {
349
350
		// Tell get_gravityview() to display a single entry
351
		add_filter( 'gravityview/is_single_entry', array( $this, 'set_single_entry_id' ) );
352
353
		ob_start();
354
355
		// Print the entry as configured in View
356
		the_gravityview( $this->view_id );
357
358
		$view_html = ob_get_clean();
359
360
		// Clean up the filter
361
		remove_filter( 'gravityview/is_single_entry', array( $this, 'set_single_entry_id' ) );
362
363
		// Strip the new lines that are generated--when editing an entry in particular, scripts are printed that
364
		// then are passed through wpautop() and everything looks terrible.
365
		$view_html = str_replace( "\n", ' ', $view_html );
366
367
		return $view_html;
368
	}
369
370
	/**
371
	 * Tell get_gravityview() to display a single entry
372
	 *
373
	 * REQUIRED FOR THE VIEW TO OUTPUT A SINGLE ENTRY
374
	 *
375
	 * @param bool|int $is_single_entry Existing single entry. False, because GV thinks we're in a post or page.
376
	 *
377
	 * @return int The current entry ID
378
	 */
379
	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...
380
381
		return $this->entry_id;
382
	}
383
384
	/**
385
	 * GravityView embed entry handler
386
	 *
387
	 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
388
	 * @param array $attr Embed attributes.
389
	 * @param string $url The original URL that was matched by the regex.
390
	 * @param array $rawattr The original unmodified attributes.
391
	 * @return string The embed HTML.
392
	 */
393
	private function render_frontend( $matches, $attr, $url, $rawattr ) {
394
395
		// If it's already been parsed, don't re-output it.
396
		if( !empty( $this->output[ $this->entry_id ] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
397
			return $this->output[ $this->entry_id ];
398
		}
399
400
		$entry_output = $this->generate_entry_output();
401
402
		// Wrap a container div around the output to allow for custom styling
403
		$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...
404
405
		/**
406
		 * @filter `gravityview/oembed/entry` Filter the output of the oEmbed entry embed
407
		 * @param string $output HTML of the embedded entry, with wrapper div
408
		 * @param GravityView_oEmbed $object The current GravityView_oEmbed instance
409
		 * @param array $atts Other passed parameters and info. \n
410
		 *  @var string $entry_output HTML of just the View output, without the wrapper \n
411
		 *  @var array  $matches Capture group matches from the regex \n
412
		 *  @var array $attr Embed attributes. \n
413
		 *  @var string $url The original URL that was matched by the regex. \n
414
		 *  @var array $rawattr The original unmodified attributes.
415
		 */
416
		$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...
417
418
		unset( $entry_output );
419
420
		$this->output[ $this->entry_id ] = $output;
421
422
		return $this->output[ $this->entry_id ];
423
424
	}
425
426
}
427
428
GravityView_oEmbed::getInstance();