PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.4.1
WP Popular Posts v5.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 / Rest / Endpoint.php
wordpress-popular-posts / src / Rest Last commit date
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