Controller.php
4 years ago
Endpoint.php
4 years ago
PostsEndpoint.php
4 years ago
TaxonomiesEndpoint.php
4 years ago
ThemesEndpoint.php
4 years ago
ThumbnailsEndpoint.php
4 years ago
ViewLoggerEndpoint.php
4 years ago
WidgetEndpoint.php
4 years ago
Endpoint.php
114 lines
| 1 | <?php |
| 2 | namespace WordPressPopularPosts\Rest; |
| 3 | |
| 4 | use WordPressPopularPosts\Query; |
| 5 | |
| 6 | abstract class Endpoint extends \WP_REST_Controller { |
| 7 | |
| 8 | /** |
| 9 | * Plugin options. |
| 10 | * |
| 11 | * @var array $config |
| 12 | * @access private |
| 13 | */ |
| 14 | protected $config; |
| 15 | |
| 16 | /** |
| 17 | * Translate object. |
| 18 | * |
| 19 | * @var \WordPressPopularPosts\Translate $translate |
| 20 | * @access private |
| 21 | */ |
| 22 | protected $translate; |
| 23 | |
| 24 | /** |
| 25 | * Initializes class. |
| 26 | * |
| 27 | * @param array |
| 28 | * @param \WordPressPopularPosts\Translate |
| 29 | * @param \WordPressPopularPosts\Output |
| 30 | */ |
| 31 | public function __construct(array $config, \WordPressPopularPosts\Translate $translate) |
| 32 | { |
| 33 | $this->config = $config; |
| 34 | $this->translate = $translate; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Registers the endpoint(s). |
| 39 | * |
| 40 | * @since 5.3.0 |
| 41 | */ |
| 42 | abstract public function register(); |
| 43 | |
| 44 | /** |
| 45 | * Gets Query object from cache if it exists, |
| 46 | * otherwise a new Query object will be |
| 47 | * instantiated and returned. |
| 48 | * |
| 49 | * @since 5.0.3 |
| 50 | * @param array |
| 51 | * @return Query |
| 52 | */ |
| 53 | protected function maybe_query(array $params) |
| 54 | { |
| 55 | // Return cached results |
| 56 | if ( $this->config['tools']['cache']['active'] ) { |
| 57 | $key = 'wpp_' . md5(json_encode($params)); |
| 58 | $query = \WordPressPopularPosts\Cache::get($key); |
| 59 | |
| 60 | if ( false === $query ) { |
| 61 | $query = new Query($params); |
| 62 | |
| 63 | $time_value = $this->config['tools']['cache']['interval']['value']; |
| 64 | $time_unit = $this->config['tools']['cache']['interval']['time']; |
| 65 | |
| 66 | // No popular posts found, check again in 1 minute |
| 67 | if ( ! $query->get_posts() ) { |
| 68 | $time_value = 1; |
| 69 | $time_unit = 'minute'; |
| 70 | } |
| 71 | |
| 72 | \WordPressPopularPosts\Cache::set( |
| 73 | $key, |
| 74 | $query, |
| 75 | $time_value, |
| 76 | $time_unit |
| 77 | ); |
| 78 | } |
| 79 | } // Get real-time popular posts |
| 80 | else { |
| 81 | $query = new Query($params); |
| 82 | } |
| 83 | |
| 84 | return $query; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sets language/locale. |
| 89 | * |
| 90 | * @since 5.3.0 |
| 91 | */ |
| 92 | protected function set_lang($lang) |
| 93 | { |
| 94 | // Multilang support |
| 95 | if ( $lang ) { |
| 96 | $current_locale = get_locale(); |
| 97 | $locale = null; |
| 98 | |
| 99 | // Polylang support |
| 100 | if ( function_exists('PLL') ) { |
| 101 | $lang_object = PLL()->model->get_language($lang); |
| 102 | $locale = ( $lang_object && isset($lang_object->locale) ) ? $lang_object->locale : null; |
| 103 | } |
| 104 | |
| 105 | // Reload locale if needed |
| 106 | if ( $locale && $locale != $current_locale ) { |
| 107 | $this->translate->set_current_language($lang); |
| 108 | unload_textdomain('wordpress-popular-posts'); |
| 109 | load_textdomain('wordpress-popular-posts', WP_LANG_DIR . '/plugins/wordpress-popular-posts-' . $locale . '.mo'); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 |