PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / system / class-statistics.php

class-statistics.php in DecaLog 4.4.0, at includes/system/class-statistics.php

162 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin statistics handling
4 *
5 * Handles all user plugin statistics and detection.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.3.0
10 */
11
12 namespace Decalog\System;
13
14 /**
15 * Define the plugin statistics functionality.
16 *
17 * Handles all plugin statistics operations and detection.
18 *
19 * @package System
20 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
21 * @since 1.3.0
22 */
23 class Statistics {
24
25 /**
26 * The unique instance of the class.
27 *
28 * @since 1.3.0
29 * @var self $instance The unique instance of the class.
30 */
31 private static $instance;
32
33 /**
34 * The number of active installs for this plugin.
35 *
36 * @since 1.3.0
37 * @var integer $installs The number of active installs.
38 */
39 public $installs = -1;
40
41 /**
42 * The number of active downloads for this plugin.
43 *
44 * @since 1.3.0
45 * @var integer $downloads The number of downloads.
46 */
47 public $downloads = -1;
48
49 /**
50 * The rating of this plugin.
51 *
52 * @since 1.3.0
53 * @var integer $rating The rating.
54 */
55 public $rating = -1;
56
57 /**
58 * The number of reviews for this plugin.
59 *
60 * @since 1.3.0
61 * @var integer $reviews The number of reviews.
62 */
63 public $reviews = -1;
64
65 /**
66 * Initializes the class and set its properties.
67 *
68 * @since 1.3.0
69 */
70 public function __construct() {
71 $this->get_wp_stats();
72 }
73
74 /**
75 * Get the WP stats about active installs, downloads, rating and reviews.
76 *
77 * @since 1.3.0
78 */
79 private function get_wp_stats() {
80 $stats = Cache::get_global( '/Plugin/WPstats' );
81 if ( ! $stats ) {
82 try {
83 if ( ! function_exists( 'plugins_api' ) ) {
84 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
85 }
86 $query = [
87 'active_installs' => true,
88 'downloaded' => true,
89 'rating' => true,
90 'num_ratings' => true,
91 ];
92 $api = plugins_api(
93 'plugin_information',
94 [
95 'slug' => DECALOG_SLUG,
96 'fields' => $query,
97 ]
98 );
99 if ( ! is_wp_error( $api ) ) {
100 $result = get_object_vars( $api );
101 Cache::set_global( '/Plugin/WPstats', $result, 'plugin-statistics' );
102 $stats = $result;
103 } else {
104 $stats = false;
105 }
106 } catch ( \Throwable $ex ) {
107 $stats = false;
108 }
109 }
110 if ( false !== $stats ) {
111 if ( array_key_exists( 'active_installs', $stats ) ) {
112 $this->installs = $stats['active_installs'];
113 }
114 if ( array_key_exists( 'downloaded', $stats ) ) {
115 $this->downloads = $stats['downloaded'];
116 }
117 if ( array_key_exists( 'rating', $stats ) ) {
118 $this->rating = $stats['rating'];
119 }
120 if ( array_key_exists( 'num_ratings', $stats ) ) {
121 $this->reviews = $stats['num_ratings'];
122 }
123 }
124 }
125
126 /**
127 * Get the statistics as shortcode.
128 *
129 * @param array $attributes Attributes of the shortcode.
130 * @return int|string The output of the shortcode, ready to print.
131 * @since 1.3.0
132 */
133 public static function sc_get_raw( $attributes ) {
134 $_attributes = shortcode_atts(
135 [
136 'item' => 'rating',
137 ],
138 $attributes
139 );
140 if ( ! isset( self::$instance ) ) {
141 self::$instance = new Statistics();
142 }
143 switch ( $_attributes['item'] ) {
144 case 'installs':
145 $result = self::$instance->installs;
146 break;
147 case 'downloads':
148 $result = self::$instance->downloads;
149 break;
150 case 'rating':
151 $result = self::$instance->rating;
152 break;
153 case 'reviews':
154 $result = self::$instance->reviews;
155 break;
156 default:
157 $result = '';
158 }
159 return $result;
160 }
161 }
162