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