Completed
Push — master ( 5181a0...3a4eff )
by David
02:49
created

Wordlift_Public::enqueue_styles()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 2
nop 0
dl 0
loc 32
rs 8.7857
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
	 * @since    1.0.0
47
	 *
48
	 * @param      string $plugin_name The name of the plugin.
49
	 * @param      string $version The version of this plugin.
50
	 */
51
	public function __construct( $plugin_name, $version ) {
52
53
		$this->plugin_name = $plugin_name;
54
		$this->version     = $version;
55
56
	}
57
58
	/**
59
	 * Register the stylesheets for the public-facing side of the site.
60
	 *
61
	 * @since 3.19.3 Register the `wordlift-ui` css.
62
	 * @since 3.19.2 The call to this function is commented out in `class-wordlift.php` because `wordlift-public.css`
63
	 *               is empty.
64
	 * @since 1.0.0
65
	 */
66
	public function enqueue_styles() {
67
68
		/**
69
		 * An instance of this class should be passed to the run() function
70
		 * defined in Wordlift_Loader as all of the hooks are defined
71
		 * in that particular class.
72
		 *
73
		 * The Wordlift_Loader will then create the relationship
74
		 * between the defined hooks and the functions defined in this
75
		 * class.
76
		 */
77
78
		/**
79
		 * Add the `wordlift-font-awesome` unless some 3rd party sets the flag to false.
80
		 *
81
		 * @since 3.19.3
82
		 *
83
		 * @param bool $include Whether to include or not font-awesome (default true).
84
		 */
85
		$deps = apply_filters( 'wl_include_font_awesome', true )
86
			? array( 'wordlift-font-awesome' )
87
			: array();
88
		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' );
89
		wp_register_style( 'wordlift-ui', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', $deps, $this->version, 'all' );
90
91
		// You need to re-enable the enqueue_styles in `class-wordlift.php` to make this effective.
92
		//
93
		// @see https://github.com/insideout10/wordlift-plugin/issues/821
94
		//
95
		// wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
97
	}
98
99
	/**
100
	 * Register the stylesheets for the public-facing side of the site.
101
	 *
102
	 * @since    1.0.0
103
	 */
104
	public function enqueue_scripts() {
105
106
		/**
107
		 * This function is provided for demonstration purposes only.
108
		 *
109
		 * An instance of this class should be passed to the run() function
110
		 * defined in Wordlift_Loader as all of the hooks are defined
111
		 * in that particular class.
112
		 *
113
		 * The Wordlift_Loader will then create the relationship
114
		 * between the defined hooks and the functions defined in this
115
		 * class.
116
		 */
117
118
		$settings = self::get_settings();
119
120
		// Note that we switched the js to be loaded in footer, since it is loading
121
		// the json-ld representation.
122
		wp_enqueue_script( $this->plugin_name, self::get_public_js_url(), array(), $this->version, true );
123
		wp_localize_script( $this->plugin_name, 'wlSettings', $settings );
124
125
		/*
126
		 * Add WordLift's version.
127
		 *
128
		 * @since 3.19.4
129
		 *
130
		 * @see https://github.com/insideout10/wordlift-plugin/issues/843.
131
		 */
132
		wp_localize_script( $this->plugin_name, 'wordlift', array(
133
			'version' => $this->version,
134
		) );
135
136
	}
137
138
	/**
139
	 * Get the settings array.
140
	 *
141
	 * @since 3.19.1
142
	 *
143
	 * @return array An array with the settings.
144
	 */
145
	public static function get_settings() {
146
147
		// Prepare a settings array for client-side functions.
148
		$settings = array(
149
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
150
			'apiUrl'  => get_home_url( null, 'wl-api/' ),
151
		);
152
153
		// If we're in a single page, then print out the post id.
154
		if ( is_singular() ) {
155
			$settings['postId'] = get_the_ID();
156
		}
157
158
		// Add flag that we are on home/blog page.
159
		if ( is_home() || is_front_page() ) {
160
			$settings['isHome'] = true;
161
		}
162
163
		// By default only enable JSON-LD on supported entity pages (includes
164
		// `page`, `post` and `entity` by default) and on the home page.
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
165
		//
166
		// @see https://github.com/insideout10/wordlift-plugin/issues/733
167
		$jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() );
168
169
		// Add the JSON-LD enabled flag, when set to false, the JSON-lD won't
170
		// be loaded.
171
		//
172
		// @see https://github.com/insideout10/wordlift-plugin/issues/642.
173
		$settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled );
174
175
		return $settings;
176
	}
177
178
	/**
179
	 * Get the public JavaScript URL.
180
	 *
181
	 * Using this function is encouraged, since the public JavaScript is also used by the {@link Wordlift_WpRocket_Adapter}
182
	 * in order to avoid breaking optimizations.
183
	 *
184
	 * @since 3.19.4
185
	 *
186
	 * @see https://github.com/insideout10/wordlift-plugin/issues/842.
187
	 *
188
	 * @return string The URL to the public JavaScript.
189
	 */
190
	public static function get_public_js_url() {
191
192
		return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js';
193
	}
194
195
}
196