Completed
Push — develop ( 5c7568...6a9ba1 )
by David
14:52
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, plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js', array(), $this->version, true );
123
		wp_localize_script( $this->plugin_name, 'wlSettings', $settings );
124
125
	}
126
127
	/**
128
	 * Get the settings array.
129
	 *
130
	 * @since 3.19.1
131
	 *
132
	 * @return array An array with the settings.
133
	 */
134
	public static function get_settings() {
135
136
		// Prepare a settings array for client-side functions.
137
		$settings = array(
138
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
139
			'apiUrl'  => get_home_url( null, 'wl-api/' ),
140
		);
141
142
		// If we're in a single page, then print out the post id.
143
		if ( is_singular() ) {
144
			$settings['postId'] = get_the_ID();
145
		}
146
147
		// Add flag that we are on home/blog page.
148
		if ( is_home() || is_front_page() ) {
149
			$settings['isHome'] = true;
150
		}
151
152
		// By default only enable JSON-LD on supported entity pages (includes
153
		// `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...
154
		//
155
		// @see https://github.com/insideout10/wordlift-plugin/issues/733
156
		$jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() );
157
158
		// Add the JSON-LD enabled flag, when set to false, the JSON-lD won't
159
		// be loaded.
160
		//
161
		// @see https://github.com/insideout10/wordlift-plugin/issues/642.
162
		$settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled );
163
164
		return $settings;
165
	}
166
167
}
168