Completed
Push — develop ( fc3971...71605d )
by David
03:09 queued 11s
created

Wordlift_Public::get_settings()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
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
		 * Context Cards and Navigator
148
		 *
149
		 * @since 3.22.0
150
		 *
151
		 */
152
		wp_register_script( 'wordlift-cloud', self::get_cloud_js_url(), array(), Wordlift::get_instance()->get_version(), true );
153
154
	}
155
156
	/**
157
	 * Get the settings array.
158
	 *
159
	 * @return array An array with the settings.
160
	 * @since 3.19.1
161
	 *
162
	 */
163
	public static function get_settings() {
164
165
		// Prepare a settings array for client-side functions.
166
		$settings = array(
167
			'ajaxUrl'    => admin_url( 'admin-ajax.php' ),
168
			'apiUrl'     => get_home_url( null, 'wl-api/' ),
169
			'jsonld_url' => rest_url( '/wordlift/v1/jsonld/' )
170
		);
171
172
		// If we're in a single page, then print out the post id.
173
		if ( is_singular() ) {
174
			$settings['postId'] = get_the_ID();
175
		}
176
177
		// Add flag that we are on home/blog page.
178
		if ( is_home() || is_front_page() ) {
179
			$settings['isHome'] = true;
180
		}
181
182
		// As of 2020-02-15, we publish the JSON-LD in the head, see Jsonld_Adaper.
183
		$settings['jsonld_enabled'] = false;
184
185
		// By default only enable JSON-LD on supported entity pages (includes
186
		// `page`, `post` and `entity` by default) and on the home page.
187
		//
188
		// @see https://github.com/insideout10/wordlift-plugin/issues/733
189
		//	$jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() );
190
191
		// Add the JSON-LD enabled flag, when set to false, the JSON-LD won't
192
		// be loaded.
193
		//
194
		// @see https://github.com/insideout10/wordlift-plugin/issues/642.
195
		// $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled );
196
197
		return $settings;
198
	}
199
200
	/**
201
	 * Get the public JavaScript URL.
202
	 *
203
	 * Using this function is encouraged, since the public JavaScript is also used by the {@link Wordlift_WpRocket_Adapter}
204
	 * in order to avoid breaking optimizations.
205
	 *
206
	 * @return string The URL to the public JavaScript.
207
	 * @see https://github.com/insideout10/wordlift-plugin/issues/842.
208
	 *
209
	 * @since 3.19.4
210
	 *
211
	 */
212
	public static function get_public_js_url() {
213
214
		return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js';
215
	}
216
217
	/**
218
	 * Get the Cloud JavaScript URL.
219
	 *
220
	 * @see https://github.com/insideout10/wordlift-plugin/issues/971
221
	 * @since 3.23.0
222
	 * @return string The URL to the Cloud JavaScript.
223
	 */
224
	public static function get_cloud_js_url() {
225
226
		return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/wordlift-cloud.js';
227
	}
228
}
229