| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || exit; |
| 3 |
|
| 4 |
if ( ! class_exists( 'LP_Background_Query_Items' ) ) { |
| 5 |
/** |
| 6 |
* Class LP_Background_Query_Items |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
class LP_Background_Query_Items { |
| 11 |
|
| 12 |
/** |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $action = 'query_items'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var int |
| 19 |
*/ |
| 20 |
protected $queue_lock_time = 3600; |
| 21 |
|
| 22 |
protected $cron_interval = 60 * 24; // minutes |
| 23 |
|
| 24 |
/** |
| 25 |
* @var float|int |
| 26 |
*/ |
| 27 |
protected $transient_time = 0; |
| 28 |
|
| 29 |
/** |
| 30 |
* LP_Background_Query_Items constructor. |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
//parent::__construct(); |
| 34 |
$this->transient_time = DAY_IN_SECONDS / 2; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param mixed $data |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
protected function task( $data ) { |
| 43 |
//parent::task( $data ); |
| 44 |
|
| 45 |
if ( ! isset( $data['callback'] ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! is_callable( array( $this, $data['callback'] ) ) ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
call_user_func( array( $this, $data['callback'] ) ); |
| 54 |
|
| 55 |
delete_option( 'doing_' . $data['callback'] ); |
| 56 |
|
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
public function get_plugins_from_wp() { |
| 61 |
$plugins = get_transient( 'lp_plugins_wp' ); |
| 62 |
|
| 63 |
if ( ! $plugins ) { |
| 64 |
$this->query_free_addons(); |
| 65 |
} |
| 66 |
|
| 67 |
return is_array( $plugins ) ? $plugins : false; |
| 68 |
} |
| 69 |
|
| 70 |
public function get_plugins_from_tp() { |
| 71 |
$plugins = get_transient( 'lp_plugins_tp' ); |
| 72 |
|
| 73 |
if ( ! $plugins ) { |
| 74 |
$this->query_premium_addons(); |
| 75 |
} |
| 76 |
|
| 77 |
return is_array( $plugins ) ? $plugins : false; |
| 78 |
} |
| 79 |
|
| 80 |
public function get_related_themes() { |
| 81 |
$themes = get_transient( 'lp_related_themes' ); |
| 82 |
|
| 83 |
if ( ! $themes ) { |
| 84 |
$this->query_related_themes(); |
| 85 |
} |
| 86 |
|
| 87 |
return is_array( $themes ) ? $themes : false; |
| 88 |
} |
| 89 |
|
| 90 |
public function force_update() { |
| 91 |
$transients = array( 'lp_plugins_wp', 'lp_plugins_tp', 'lp_related_themes' ); |
| 92 |
|
| 93 |
foreach ( $transients as $transient ) { |
| 94 |
delete_transient( $transient ); |
| 95 |
} |
| 96 |
|
| 97 |
$this->query_free_addons(); |
| 98 |
$this->query_premium_addons(); |
| 99 |
$this->query_related_themes(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Query all free addons from wp.org |
| 104 |
* |
| 105 |
* @return array|string |
| 106 |
*/ |
| 107 |
public function query_free_addons() { |
| 108 |
LP_Plugins_Helper::require_plugins_api(); |
| 109 |
|
| 110 |
$per_page = 20; |
| 111 |
$paged = 1; |
| 112 |
$tag = 'learnpress'; |
| 113 |
|
| 114 |
$query_args = array( |
| 115 |
'page' => $paged, |
| 116 |
'per_page' => $per_page, |
| 117 |
'fields' => array( |
| 118 |
'last_updated' => true, |
| 119 |
'icons' => true, |
| 120 |
'active_installs' => true, |
| 121 |
), |
| 122 |
'locale' => get_locale(), |
| 123 |
'installed_plugins' => LP_Plugins_Helper::get_installed_plugin_slugs(), |
| 124 |
'author' => 'thimpress', |
| 125 |
); |
| 126 |
$plugins = array(); |
| 127 |
|
| 128 |
set_transient( 'lp_plugins_wp', __( 'There is no items found!', 'learnpress' ), $this->transient_time ); |
| 129 |
|
| 130 |
try { |
| 131 |
$api = plugins_api( 'query_plugins', $query_args ); |
| 132 |
if ( is_wp_error( $api ) ) { |
| 133 |
throw new Exception( __( 'WP query plugins error!', 'learnpress' ) ); |
| 134 |
} |
| 135 |
if ( ! is_array( $api->plugins ) ) { |
| 136 |
throw new Exception( __( 'WP query plugins empty!', 'learnpress' ) ); |
| 137 |
} |
| 138 |
$all_plugins = get_plugins(); |
| 139 |
// Filter plugins with tag contains 'learnpress' |
| 140 |
$_plugins = array_filter( $api->plugins, array( 'LP_Plugins_Helper', '_filter_plugin' ) ); |
| 141 |
|
| 142 |
// Ensure that the array is indexed from 0 |
| 143 |
$_plugins = array_values( $_plugins ); |
| 144 |
|
| 145 |
for ( $n = sizeof( $_plugins ), $i = $n - 1; $i >= 0; $i -- ) { |
| 146 |
$plugin = $_plugins[ $i ]; |
| 147 |
|
| 148 |
if ( ! isset( $plugin->slug ) ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$key = $plugin->slug; |
| 153 |
foreach ( $all_plugins as $file => $p ) { |
| 154 |
if ( strpos( $file, $plugin->slug ) !== false ) { |
| 155 |
$key = $file; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
$plugin->source = 'wp'; |
| 160 |
$plugins[ $key ] = (array) $plugin; |
| 161 |
} |
| 162 |
|
| 163 |
if ( sizeof( $plugins ) ) { |
| 164 |
set_transient( 'lp_plugins_wp', $plugins, $this->transient_time ); |
| 165 |
} |
| 166 |
} catch ( Exception $ex ) { |
| 167 |
} |
| 168 |
|
| 169 |
return $plugins; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Query all available premium addons from thimpress.com |
| 174 |
* |
| 175 |
* @return array|bool |
| 176 |
*/ |
| 177 |
public function query_premium_addons() { |
| 178 |
$plugins = array(); |
| 179 |
$url = 'https://thimpress.com/?thimpress_get_addons=premium'; |
| 180 |
$response = wp_remote_get( esc_url_raw( $url ), array( 'decompress' => false ) ); |
| 181 |
|
| 182 |
set_transient( 'lp_plugins_tp', __( 'There is no items found!', 'learnpress' ), $this->transient_time ); |
| 183 |
|
| 184 |
if ( is_wp_error( $response ) ) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
$response = wp_remote_retrieve_body( $response ); |
| 189 |
$response = json_decode( $response, true ); |
| 190 |
|
| 191 |
if ( ! empty( $response ) ) { |
| 192 |
$maps = array( |
| 193 |
'authorize-net-add-on-learnpress' => 'learnpress-authorizenet-payment', |
| 194 |
'2checkout-add-learnpress' => 'learnpress-2checkout-payment', |
| 195 |
'commission-add-on-for-learnpress' => 'learnpress-commission', |
| 196 |
'paid-memberships-pro-add-learnpress' => 'learnpress-paid-membership-pro', |
| 197 |
'gradebook-add-on-for-learnpress' => 'learnpress-gradebook', |
| 198 |
'sorting-choice-add-on-for-learnpress' => 'learnpress-sorting-choice', |
| 199 |
'content-drip-add-on-for-learnpress' => 'learnpress-content-drip', |
| 200 |
'mycred-add-on-for-learnpress' => 'learnpress-mycred', |
| 201 |
'random-quiz-add-on-for-learnpress' => 'learnpress-random-quiz', |
| 202 |
'co-instructors-add-on-for-learnpress' => 'learnpress-co-instructor', |
| 203 |
'collections-add-on-for-learnpress' => 'learnpress-collections', |
| 204 |
'woocommerce-add-on-for-learnpress' => 'learnpress-woo-payment', |
| 205 |
'stripe-add-on-for-learnpress' => 'learnpress-stripe', |
| 206 |
'certificates-add-on-for-learnpress' => 'learnpress-certificates', |
| 207 |
'assignments-add-on-for-learnpress' => 'learnpress-assignments', |
| 208 |
'announcement-add-on-for-learnpress' => 'learnpress-announcements', |
| 209 |
); |
| 210 |
|
| 211 |
foreach ( $response as $key => $item ) { |
| 212 |
$slug = $item['slug']; |
| 213 |
if ( ! empty( $maps[ $slug ] ) ) { |
| 214 |
$plugin_file = sprintf( '%1$s/%1$s.php', $maps[ $slug ] ); |
| 215 |
$plugins[ $plugin_file ] = $item; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if ( sizeof( $plugins ) ) { |
| 220 |
set_transient( 'lp_plugins_tp', $plugins, $this->transient_time ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return $plugins; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* @return array|bool |
| 229 |
*/ |
| 230 |
public function query_related_themes() { |
| 231 |
|
| 232 |
set_transient( 'lp_related_themes', __( 'There is no item found!', 'learnpress' ), $this->transient_time ); |
| 233 |
|
| 234 |
$themes = array(); |
| 235 |
$url = 'https://api.envato.com/v1/discovery/search/search/item?site=themeforest.net&username=thimpress'; |
| 236 |
$args = array( |
| 237 |
'headers' => array( |
| 238 |
'Authorization' => 'Bearer BmYcBsYXlSoVe0FekueDxqNGz2o3JRaP', |
| 239 |
), |
| 240 |
); |
| 241 |
$response = wp_remote_request( $url, $args ); |
| 242 |
|
| 243 |
if ( is_wp_error( $response ) ) { |
| 244 |
error_log( $response->get_error_message() ); |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
$response = wp_remote_retrieve_body( $response ); |
| 249 |
$response = json_decode( $response, true ); |
| 250 |
|
| 251 |
if ( ! empty( $response ) && ! empty( $response['matches'] ) ) { |
| 252 |
$all_themes = array(); |
| 253 |
|
| 254 |
foreach ( $response['matches'] as $theme ) { |
| 255 |
$all_themes[ $theme['id'] ] = $theme; |
| 256 |
} |
| 257 |
|
| 258 |
$education_themes = apply_filters( |
| 259 |
'learn-press/education-themes', |
| 260 |
array( |
| 261 |
'23451388' => 'kindergarten', |
| 262 |
'22773871' => 'ivy-school', |
| 263 |
'20370918' => 'wordpress-lms', |
| 264 |
'14058034' => 'eduma', |
| 265 |
'17097658' => 'coach', |
| 266 |
'11797847' => 'lms', |
| 267 |
) |
| 268 |
); |
| 269 |
|
| 270 |
if ( $education_themes ) { |
| 271 |
$themes['other'] = array_diff_key( $all_themes, $education_themes ); |
| 272 |
$themes['education'] = array_diff_key( $all_themes, $themes['other'] ); |
| 273 |
} else { |
| 274 |
$themes['other'] = $all_themes; |
| 275 |
} |
| 276 |
|
| 277 |
set_transient( 'lp_related_themes', $themes, $this->transient_time ); |
| 278 |
|
| 279 |
} elseif ( ! empty( $response['message'] ) ) { |
| 280 |
set_transient( 'lp_related_themes', $response['message'], $this->transient_time ); |
| 281 |
} |
| 282 |
|
| 283 |
return $themes; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* @return LP_Background_Query_Items |
| 288 |
*/ |
| 289 |
public static function instance() { |
| 290 |
static $instance; |
| 291 |
if ( empty( $instance ) ) { |
| 292 |
$instance = new self(); |
| 293 |
} |
| 294 |
|
| 295 |
return $instance; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return LP_Background_Query_Items::instance(); |
| 301 |
|