PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 6.4.1
WP Popular Posts v6.4.1
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Front / Front.php
wordpress-popular-posts / src / Front Last commit date
Front.php 2 years ago
Front.php
178 lines
1 <?php
2 /**
3 * The public-facing functionality of the plugin.
4 *
5 * Defines hooks to enqueue the public-specific stylesheet and JavaScript.
6 *
7 * @package WordPressPopularPosts
8 * @subpackage WordPressPopularPosts/Front
9 * @author Hector Cabrera <me@cabrerahector.com>
10 */
11
12 namespace WordPressPopularPosts\Front;
13
14 use WordPressPopularPosts\{ Helper, Translate };
15
16 class Front {
17
18 /**
19 * Plugin options.
20 *
21 * @var array $config
22 * @access private
23 */
24 private $config;
25
26 /**
27 * Translate object.
28 *
29 * @var \WordPressPopularPosts\Translate $translate
30 * @access private
31 */
32 private $translate;
33
34 /**
35 * Construct.
36 *
37 * @since 5.0.0
38 * @param array $config Admin settings.
39 * @param \WordPressPopularPosts\Translate $translate Translate class.
40 */
41 public function __construct(array $config, Translate $translate)
42 {
43 $this->config = $config;
44 $this->translate = $translate;
45 }
46
47 /**
48 * WordPress public-facing hooks.
49 *
50 * @since 5.0.0
51 */
52 public function hooks()
53 {
54 add_action('wp_head', [$this, 'inline_loading_css']);
55 add_action('wp_enqueue_scripts', [$this, 'enqueue_assets']);
56 add_filter('script_loader_tag', [$this, 'convert_inline_js_into_json'], 10, 3);
57 }
58
59 /**
60 * Inserts CSS related to the loading animation into <head>
61 *
62 * @since 5.3.0
63 */
64 public function inline_loading_css()
65 {
66 $wpp_insert_loading_animation_styles = apply_filters('wpp_insert_loading_animation_styles', true);
67
68 if ( $wpp_insert_loading_animation_styles ) :
69 ?>
70 <style id="wpp-loading-animation-styles">@-webkit-keyframes bgslide{from{background-position-x:0}to{background-position-x:-200%}}@keyframes bgslide{from{background-position-x:0}to{background-position-x:-200%}}.wpp-widget-placeholder,.wpp-widget-block-placeholder,.wpp-shortcode-placeholder{margin:0 auto;width:60px;height:3px;background:#dd3737;background:linear-gradient(90deg,#dd3737 0%,#571313 10%,#dd3737 100%);background-size:200% auto;border-radius:3px;-webkit-animation:bgslide 1s infinite linear;animation:bgslide 1s infinite linear}</style>
71 <?php
72 endif;
73 }
74
75 /**
76 * Enqueues public facing assets.
77 *
78 * @since 5.0.0
79 */
80 public function enqueue_assets()
81 {
82 // Enqueue WPP's stylesheet.
83 if ( $this->config['tools']['css'] ) {
84 $theme_file = get_stylesheet_directory() . '/wpp.css';
85
86 if ( @is_file($theme_file) ) {
87 wp_enqueue_style('wordpress-popular-posts-css', get_stylesheet_directory_uri() . '/wpp.css', [], WPP_VERSION, 'all');
88 } // Load stock stylesheet
89 else {
90 wp_enqueue_style('wordpress-popular-posts-css', plugin_dir_url(dirname(dirname(__FILE__))) . 'assets/css/wpp.css', [], WPP_VERSION, 'all');
91 }
92 }
93
94 // Enqueue WPP's library.
95 $is_single = 0;
96
97 if (
98 ( 0 == $this->config['tools']['log']['level'] && ! is_user_logged_in() )
99 || ( 1 == $this->config['tools']['log']['level'] )
100 || ( 2 == $this->config['tools']['log']['level'] && is_user_logged_in() )
101 ) {
102 $is_single = Helper::is_single();
103 }
104
105 $wpp_js = ( defined('WP_DEBUG') && WP_DEBUG )
106 ? plugin_dir_url(dirname(dirname(__FILE__))) . 'assets/js/wpp.js'
107 : plugin_dir_url(dirname(dirname(__FILE__))) . 'assets/js/wpp.min.js';
108
109 wp_register_script('wpp-js', $wpp_js, [], WPP_VERSION, false);
110 $params = [
111 'sampling_active' => (int) $this->config['tools']['sampling']['active'],
112 'sampling_rate' => (int) $this->config['tools']['sampling']['rate'],
113 'ajax_url' => esc_url_raw(rest_url('wordpress-popular-posts/v1/popular-posts')),
114 'api_url' => esc_url_raw(rest_url('wordpress-popular-posts')),
115 'ID' => (int) $is_single,
116 'token' => wp_create_nonce('wp_rest'),
117 'lang' => function_exists('PLL') ? $this->translate->get_current_language() : 0,
118 'debug' => (int) WP_DEBUG
119 ];
120 wp_enqueue_script('wpp-js');
121 wp_add_inline_script('wpp-js', json_encode($params), 'before');
122 }
123
124 /**
125 * Converts inline script tag into type=application/json.
126 *
127 * This function mods the original script tag as printed
128 * by WordPress which contains the data for the wpp_params
129 * object into a JSON script. This improves compatibility
130 * with Content Security Policy (CSP).
131 *
132 * @since 5.2.0
133 * @param string $tag
134 * @param string $handle
135 * @param string $src
136 * @return string $tag
137 */
138 public function convert_inline_js_into_json(string $tag, string $handle, string $src)
139 {
140 if ( 'wpp-js' === $handle ) {
141 // id attribute found, replace it
142 if ( false !== strpos($tag, 'wpp-js-js-before') ) {
143 $tag = str_replace('wpp-js-js-before', 'wpp-json', $tag);
144 } // id attribute missing, let's add it
145 else {
146 $pos = strpos($tag, '>');
147 $tag = substr_replace($tag, ' id="wpp-json">', $pos, 1);
148 }
149
150 // type attribute found, replace it
151 if ( false !== strpos($tag, 'type') ) {
152 $pos = strpos($tag, 'text/javascript');
153
154 if ( false !== $pos ) {
155 $tag = substr_replace($tag, 'application/json', $pos, strlen('text/javascript'));
156 }
157 } // type attribute missing, let's add it
158 else {
159 $pos = strpos($tag, '>');
160 $tag = substr_replace($tag, ' type="application/json">', $pos, 1);
161 }
162
163 /**
164 * Remove CDATA block added automatically by WordPress 6.4
165 * to themes that don't support HTML5 script tags.
166 */
167 $is_html5 = current_theme_supports('html5', 'script');
168
169 if ( ! $is_html5 ) {
170 $tag = str_replace('/* <![CDATA[ */', '', $tag);
171 $tag = str_replace('/* ]]> */', '', $tag);
172 }
173 }
174
175 return $tag;
176 }
177 }
178