Front.php
128 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_head', [$this, 'enqueue_scripts'], 2); |
| 56 | add_action('wp_enqueue_scripts', [$this, 'enqueue_styles']); |
| 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-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 styles. |
| 77 | * |
| 78 | * @since 5.0.0 |
| 79 | */ |
| 80 | public function enqueue_styles() |
| 81 | { |
| 82 | if ( $this->config['tools']['css'] ) { |
| 83 | $theme_file = get_stylesheet_directory() . '/wpp.css'; |
| 84 | |
| 85 | if ( @is_file($theme_file) ) { |
| 86 | wp_enqueue_style('wordpress-popular-posts-css', get_stylesheet_directory_uri() . '/wpp.css', [], WPP_VERSION, 'all'); |
| 87 | } // Load stock stylesheet |
| 88 | else { |
| 89 | wp_enqueue_style('wordpress-popular-posts-css', plugin_dir_url(dirname(dirname(__FILE__))) . 'assets/css/wpp.css', [], WPP_VERSION, 'all'); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Enqueues public facing scripts. |
| 96 | * |
| 97 | * @since 7.0.0 |
| 98 | */ |
| 99 | public function enqueue_scripts() |
| 100 | { |
| 101 | $is_single = 0; |
| 102 | |
| 103 | if ( |
| 104 | ( 0 == $this->config['tools']['log']['level'] && ! is_user_logged_in() ) |
| 105 | || ( 1 == $this->config['tools']['log']['level'] ) |
| 106 | || ( 2 == $this->config['tools']['log']['level'] && is_user_logged_in() ) |
| 107 | ) { |
| 108 | $is_single = Helper::is_single(); |
| 109 | } |
| 110 | |
| 111 | $wpp_js_url = plugin_dir_url(dirname(dirname(__FILE__))) . 'assets/js/wpp.' . (! defined('WP_DEBUG') || false === WP_DEBUG ? 'min.' : '') . 'js?ver=' . WPP_VERSION; |
| 112 | |
| 113 | wp_print_script_tag( |
| 114 | [ |
| 115 | 'id' => 'wpp-js', |
| 116 | 'src' => $wpp_js_url, |
| 117 | 'data-sampling' => (int) $this->config['tools']['sampling']['active'], |
| 118 | 'data-sampling-rate' => (int) $this->config['tools']['sampling']['rate'], |
| 119 | 'data-api-url' => esc_url_raw(rest_url('wordpress-popular-posts')), |
| 120 | 'data-post-id' => (int) $is_single, |
| 121 | 'data-token' => wp_create_nonce('wp_rest'), |
| 122 | 'data-lang' => function_exists('PLL') ? $this->translate->get_current_language() : 0, |
| 123 | 'data-debug' => (int) WP_DEBUG |
| 124 | ] |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 |