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 |