Completed
Push — develop ( b5d722...1d2391 )
by
unknown
04:55 queued 02:26
created

Wordlift_Public::enqueue_scripts()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 63
rs 8.8072
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * The public-facing functionality of the plugin.
5
 *
6
 * @link       https://wordlift.io
7
 * @since      1.0.0
8
 *
9
 * @package    Wordlift
10
 * @subpackage Wordlift/public
11
 */
12
13
/**
14
 * The public-facing functionality of the plugin.
15
 *
16
 * Defines the plugin name, version, and two examples hooks for how to
17
 * enqueue the admin-specific stylesheet and JavaScript.
18
 *
19
 * @package    Wordlift
20
 * @subpackage Wordlift/public
21
 * @author     WordLift <[email protected]>
22
 */
23
class Wordlift_Public {
24
25
	/**
26
	 * The ID of this plugin.
27
	 *
28
	 * @since    1.0.0
29
	 * @access   private
30
	 * @var      string $plugin_name The ID of this plugin.
31
	 */
32
	private $plugin_name;
33
34
	/**
35
	 * The version of this plugin.
36
	 *
37
	 * @since    1.0.0
38
	 * @access   private
39
	 * @var      string $version The current version of this plugin.
40
	 */
41
	private $version;
42
43
	/**
44
	 * Initialize the class and set its properties.
45
	 *
46
	 * @param string $plugin_name The name of the plugin.
47
	 * @param string $version The version of this plugin.
48
	 *
49
	 * @since    1.0.0
50
	 *
51
	 */
52
	public function __construct( $plugin_name, $version ) {
53
54
		$this->plugin_name = $plugin_name;
55
		$this->version     = $version;
56
57
	}
58
59
	/**
60
	 * Register the stylesheets for the public-facing side of the site.
61
	 *
62
	 * @since 3.19.3 Register the `wordlift-ui` css.
63
	 * @since 3.19.2 The call to this function is commented out in `class-wordlift.php` because `wordlift-public.css`
64
	 *               is empty.
65
	 * @since 1.0.0
66
	 */
67
	public function enqueue_styles() {
68
69
		/**
70
		 * An instance of this class should be passed to the run() function
71
		 * defined in Wordlift_Loader as all of the hooks are defined
72
		 * in that particular class.
73
		 *
74
		 * The Wordlift_Loader will then create the relationship
75
		 * between the defined hooks and the functions defined in this
76
		 * class.
77
		 */
78
79
		/**
80
		 * Add the `wordlift-font-awesome` unless some 3rd party sets the flag to false.
81
		 *
82
		 * @param bool $include Whether to include or not font-awesome (default true).
83
		 *
84
		 * @since 3.19.3
85
		 *
86
		 */
87
		$deps = apply_filters( 'wl_include_font_awesome', true )
88
			? array( 'wordlift-font-awesome' )
89
			: array();
90
		wp_register_style( 'wordlift-font-awesome', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-font-awesome' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', array(), $this->version, 'all' );
91
		wp_register_style( 'wordlift-ui', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', $deps, $this->version, 'all' );
92
93
		// You need to re-enable the enqueue_styles in `class-wordlift.php` to make this effective.
94
		//
95
		// @see https://github.com/insideout10/wordlift-plugin/issues/821
96
		//
97
		// wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' );
98
99
	}
100
101
	/**
102
	 * Register the stylesheets for the public-facing side of the site.
103
	 *
104
	 * @since    1.0.0
105
	 */
106
	public function enqueue_scripts() {
107
108
		/**
109
		 * This function is provided for demonstration purposes only.
110
		 *
111
		 * An instance of this class should be passed to the run() function
112
		 * defined in Wordlift_Loader as all of the hooks are defined
113
		 * in that particular class.
114
		 *
115
		 * The Wordlift_Loader will then create the relationship
116
		 * between the defined hooks and the functions defined in this
117
		 * class.
118
		 */
119
120
		$settings = self::get_settings();
121
122
		// Note that we switched the js to be loaded in footer, since it is loading
123
		// the json-ld representation.
124
		wp_enqueue_script( $this->plugin_name, self::get_public_js_url(), array(), $this->version, true );
125
		wp_localize_script( $this->plugin_name, 'wlSettings', $settings );
126
127
		/*
128
		 * Add WordLift's version.
129
		 * Can be disabled via filter 'wl_disable_version_js' since 3.21.1
130
		 *
131
		 * @since 3.19.4
132
		 *
133
		 * @see https://github.com/insideout10/wordlift-plugin/issues/843.
134
		 * @see https://github.com/insideout10/wordlift-plugin/issues/926.
135
		 */
136
		$show_version_default = false;
137
		$show_version         = apply_filters( 'wl_disable_version_js', $show_version_default );
138
139
		if ( $show_version ) {
140
			wp_localize_script( $this->plugin_name, 'wordlift', array(
141
				'version' => $this->version,
142
			) );
143
		}
144
145
		/*
146
		 * Register wordlift-cloud script which is shared by
147
		 * Navigator, Products Navigator, Faceted Search, Context Cards
148
		 *
149
		 * @since 3.22.0
150
		 *
151
		 */
152
		$deps = $this->wp_version_compare( '>=', '5.0' ) ? array( 'wp-hooks' ) : array();
153
154
		/*
155
		 * Added defer to wordlift-cloud
156
		 *
157
		 * @since 3.27.4
158
		 */
159
		add_filter( 'script_loader_tag', function ( $tag, $handle ) {
160
			if ( 'wordlift-cloud' !== $handle ) {
161
				return $tag;
162
			}
163
164
			return str_replace( ' src', ' defer="defer" src', $tag );
165
		}, 10, 2 );
166
		wp_register_script( 'wordlift-cloud', self::get_cloud_js_url(), $deps, Wordlift::get_instance()->get_version(), true );
167
168
	}
169
170
	/**
171
	 * Get the settings array.
172
	 *
173
	 * @return array An array with the settings.
174
	 * @since 3.19.1
175
	 *
176
	 */
177
	public static function get_settings() {
178
179
		// Prepare a settings array for client-side functions.
180
		$settings = array(
181
			'ajaxUrl'    => admin_url( 'admin-ajax.php' ),
182
			'apiUrl'     => get_home_url( null, 'wl-api/' ),
183
			'jsonld_url' => rest_url( '/wordlift/v1/jsonld/' )
184
		);
185
186
		// If we're in a single page, then print out the post id.
187
		if ( is_singular() ) {
188
			$settings['postId'] = get_the_ID();
189
		}
190
191
		// Add flag that we are on home/blog page.
192
		if ( is_home() || is_front_page() ) {
193
			$settings['isHome'] = true;
194
		}
195
196
		// As of 2020-02-15, we publish the JSON-LD in the head, see Jsonld_Adaper.
197
		$settings['jsonld_enabled'] = false;
198
199
		// By default only enable JSON-LD on supported entity pages (includes
200
		// `page`, `post` and `entity` by default) and on the home page.
201
		//
202
		// @see https://github.com/insideout10/wordlift-plugin/issues/733
203
		//	$jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() );
204
205
		// Add the JSON-LD enabled flag, when set to false, the JSON-LD won't
206
		// be loaded.
207
		//
208
		// @see https://github.com/insideout10/wordlift-plugin/issues/642.
209
		// $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled );
210
211
		return $settings;
212
	}
213
214
	/**
215
	 * Get the public JavaScript URL.
216
	 *
217
	 * Using this function is encouraged, since the public JavaScript is also used by the {@link Wordlift_WpRocket_Adapter}
218
	 * in order to avoid breaking optimizations.
219
	 *
220
	 * @return string The URL to the public JavaScript.
221
	 * @see https://github.com/insideout10/wordlift-plugin/issues/842.
222
	 *
223
	 * @since 3.19.4
224
	 *
225
	 */
226
	public static function get_public_js_url() {
227
228
		return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js';
229
	}
230
231
	/**
232
	 * Get the Cloud JavaScript URL.
233
	 *
234
	 * @see https://github.com/insideout10/wordlift-plugin/issues/971
235
	 * @since 3.23.0
236
	 * @return string The URL to the Cloud JavaScript.
237
	 */
238
	public static function get_cloud_js_url() {
239
240
		return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/wordlift-cloud.js';
241
	}
242
243
	/**
244
	 * Helper function to check WP version
245
	 *
246
	 * @since 3.26.0
247
	 *
248
	 * @param string $operator
249
	 * @param string $version
250
	 *
251
	 * @return mixed
252
	 */
253
	private function wp_version_compare( $operator = '>', $version = '5.0' ) {
254
		global $wp_version;
255
256
		return version_compare( $wp_version, $version, $operator );
257
	}
258
}
259