Completed
Push — master ( 9cad87...c2ab66 )
by David
07:55
created

Wordlift_Public::get_settings()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 12
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    1.0.0
62
	 */
63
	public function enqueue_styles() {
64
65
		/**
66
		 * This function is provided for demonstration purposes only.
67
		 *
68
		 * An instance of this class should be passed to the run() function
69
		 * defined in Wordlift_Loader as all of the hooks are defined
70
		 * in that particular class.
71
		 *
72
		 * The Wordlift_Loader will then create the relationship
73
		 * between the defined hooks and the functions defined in this
74
		 * class.
75
		 */
76
77
		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' );
78
79
	}
80
81
	/**
82
	 * Register the stylesheets for the public-facing side of the site.
83
	 *
84
	 * @since    1.0.0
85
	 */
86
	public function enqueue_scripts() {
87
88
		/**
89
		 * This function is provided for demonstration purposes only.
90
		 *
91
		 * An instance of this class should be passed to the run() function
92
		 * defined in Wordlift_Loader as all of the hooks are defined
93
		 * in that particular class.
94
		 *
95
		 * The Wordlift_Loader will then create the relationship
96
		 * between the defined hooks and the functions defined in this
97
		 * class.
98
		 */
99
100
		$settings = self::get_settings();
101
102
		// Note that we switched the js to be loaded in footer, since it is loading
103
		// the json-ld representation.
104
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js', array(), $this->version, true );
105
		wp_localize_script( $this->plugin_name, 'wlSettings', $settings );
106
107
	}
108
109
	/**
110
	 * Get the settings array.
111
	 *
112
	 * @since 3.19.1
113
	 *
114
	 * @return array An array with the settings.
115
	 */
116
	public static function get_settings() {
117
118
		// Prepare a settings array for client-side functions.
119
		$settings = array(
120
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
121
			'apiUrl'  => get_site_url( null, 'wl-api/' ),
122
		);
123
124
		// If we're in a single page, then print out the post id.
125
		if ( is_singular() ) {
126
			$settings['postId'] = get_the_ID();
127
		}
128
129
		// Add flag that we are on home/blog page.
130
		if ( is_home() || is_front_page() ) {
131
			$settings['isHome'] = true;
132
		}
133
134
		// By default only enable JSON-LD on supported entity pages (includes
135
		// `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...
136
		//
137
		// @see https://github.com/insideout10/wordlift-plugin/issues/733
138
		$jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() );
139
140
		// Add the JSON-LD enabled flag, when set to false, the JSON-lD won't
141
		// be loaded.
142
		//
143
		// @see https://github.com/insideout10/wordlift-plugin/issues/642.
144
		$settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled );
145
146
		return $settings;
147
	}
148
149
}
150