| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Classes; |
| 4 |
|
| 5 |
use WPIDE\App\App; |
| 6 |
use const WPIDE\Constants\AUTHOR; |
| 7 |
use const WPIDE\Constants\VERSION; |
| 8 |
|
| 9 |
class RecommendedPlugins |
| 10 |
{ |
| 11 |
|
| 12 |
public static function init() |
| 13 |
{ |
| 14 |
|
| 15 |
add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_assets'], 99); |
| 16 |
add_filter('plugins_api_result', [__CLASS__, 'plugin_results'], 1, 3); |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
public static function is_plugin_install_page(): bool |
| 21 |
{ |
| 22 |
|
| 23 |
$screen = get_current_screen(); |
| 24 |
|
| 25 |
return (!empty($screen) && ($screen->id === 'plugin-install' || $screen->id === 'plugin-install-network')); |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
public static function is_plugin_install_page_xt_tabs($_tab = false): bool |
| 30 |
{ |
| 31 |
|
| 32 |
$isPluginInstallPage = self::is_plugin_install_page(); |
| 33 |
|
| 34 |
$tab = !empty(filter_input(INPUT_POST, 'tab')) ? filter_input(INPUT_POST, 'tab') : filter_input(INPUT_GET, 'tab'); |
| 35 |
|
| 36 |
if ($_tab) { |
| 37 |
|
| 38 |
return $isPluginInstallPage && ($tab === $_tab); |
| 39 |
} |
| 40 |
|
| 41 |
return $isPluginInstallPage && $tab == 'search'; |
| 42 |
} |
| 43 |
|
| 44 |
public static function enqueue_assets() |
| 45 |
{ |
| 46 |
|
| 47 |
if (!App::instance()->isPluginScreen() && !self::is_plugin_install_page_xt_tabs()) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$handle = App::instance()->prefix('plugins'); |
| 52 |
|
| 53 |
wp_register_script( $handle, false, array('jquery'), VERSION ); |
| 54 |
|
| 55 |
wp_add_inline_script($handle, ' |
| 56 |
(function( $ ) { |
| 57 |
|
| 58 |
function alter_frame(frame) { |
| 59 |
|
| 60 |
var doc = $(frame).contents().get(0); |
| 61 |
var filter = 5; |
| 62 |
|
| 63 |
$(".counter-container", doc).each(function() { |
| 64 |
|
| 65 |
var has_reviews = parseInt($(this, doc).find(".counter-count").text().trim()) > 0; |
| 66 |
var link = $(this, doc).find(".counter-label a"); |
| 67 |
var plugin_link = $("#plugin-information-content > .fyi > ul:first-child", doc).find("li").last().find("a"); |
| 68 |
|
| 69 |
if(!has_reviews) { |
| 70 |
|
| 71 |
link.removeAttr("href"); |
| 72 |
|
| 73 |
}else if(plugin_link.length){ |
| 74 |
|
| 75 |
link.attr("href", plugin_link.attr("href")+"?filter="+filter+"#ratings"); |
| 76 |
} |
| 77 |
|
| 78 |
filter--; |
| 79 |
}); |
| 80 |
} |
| 81 |
|
| 82 |
var observer = new MutationObserver(function (mutations) { |
| 83 |
mutations.forEach(function (mutation) { |
| 84 |
[].filter.call(mutation.addedNodes, function (node) { |
| 85 |
return node.nodeName == "IFRAME" && node.id === "TB_iframeContent"; |
| 86 |
}).forEach(function (frame) { |
| 87 |
frame.addEventListener("load", function (e) { |
| 88 |
alter_frame(frame); |
| 89 |
}); |
| 90 |
}); |
| 91 |
}); |
| 92 |
}); |
| 93 |
$(document).ready(function() { |
| 94 |
observer.observe(window.document.body, { childList: true, subtree: true }); |
| 95 |
}); |
| 96 |
})( jQuery ); |
| 97 |
'); |
| 98 |
|
| 99 |
wp_enqueue_script( $handle ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Gets the current plugin page number. |
| 104 |
* |
| 105 |
* @return int |
| 106 |
*/ |
| 107 |
public static function get_pagenum(): int |
| 108 |
{ |
| 109 |
|
| 110 |
$pagenum = isset($_REQUEST['paged']) ? absint($_REQUEST['paged']) : 0; |
| 111 |
|
| 112 |
return max(1, $pagenum); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @throws \DI\DependencyException |
| 117 |
* @throws \DI\NotFoundException |
| 118 |
*/ |
| 119 |
public static function get_plugins() |
| 120 |
{ |
| 121 |
|
| 122 |
return App::instance()->cache()->result('get_plugins', function () { |
| 123 |
|
| 124 |
$result = plugins_api('query_plugins', array( |
| 125 |
'page' => self::get_pagenum(), |
| 126 |
'per_page' => 36, |
| 127 |
'author' => AUTHOR, |
| 128 |
'xt_plugins_query' => true |
| 129 |
)); |
| 130 |
|
| 131 |
$result->plugins = self::alter_plugin_results($result->plugins); |
| 132 |
|
| 133 |
return $result; |
| 134 |
|
| 135 |
}); |
| 136 |
} |
| 137 |
|
| 138 |
public static function plugin_results($res, $action, $args) |
| 139 |
{ |
| 140 |
|
| 141 |
if (is_wp_error($res)) { |
| 142 |
return $res; |
| 143 |
} |
| 144 |
|
| 145 |
if ($action === 'query_plugins') { |
| 146 |
|
| 147 |
$new_result = self::alter_query_plugins($res, $action, $args); |
| 148 |
|
| 149 |
if (is_wp_error($new_result)) { |
| 150 |
return $res; |
| 151 |
} |
| 152 |
|
| 153 |
if (self::is_plugin_install_page_xt_tabs()) { |
| 154 |
|
| 155 |
$position = 4; |
| 156 |
$position_middle = 6; |
| 157 |
$position_middle = max($position, $position_middle); |
| 158 |
|
| 159 |
$top_plugins = array_splice($res->plugins, 0, $position); |
| 160 |
$middle_plugins = array_merge($new_result->plugins, array_splice($res->plugins, $position, $position_middle)); |
| 161 |
$below_plugins = array_splice($res->plugins, $position_middle); |
| 162 |
shuffle($middle_plugins); |
| 163 |
|
| 164 |
$res->plugins = array_merge($top_plugins, $middle_plugins, $below_plugins); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
} else if ($action === 'plugin_information') { |
| 169 |
|
| 170 |
if (!empty($res->author) && strpos($res->author, AUTHOR)) { |
| 171 |
|
| 172 |
$res = json_decode(json_encode($res), true); |
| 173 |
|
| 174 |
return (object)self::alter_plugin_info($res); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return $res; |
| 179 |
} |
| 180 |
|
| 181 |
public static function search_contains($needles, $haystack): int |
| 182 |
{ |
| 183 |
return count(array_intersect($needles, explode(" ", preg_replace("/[^A-Za-z0-9' -]/", "", $haystack)))); |
| 184 |
} |
| 185 |
|
| 186 |
public static function search_terms(): array |
| 187 |
{ |
| 188 |
return array('woo', 'woocommerce', 'cart', 'quick view', 'quickview', 'points', 'rewards', 'swatches', 'attributes', 'variation', 'variations'); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @throws \DI\DependencyException |
| 193 |
* @throws \DI\NotFoundException |
| 194 |
*/ |
| 195 |
public static function alter_query_plugins($res, $action, $args) |
| 196 |
{ |
| 197 |
|
| 198 |
$args = (array)$args; |
| 199 |
|
| 200 |
if ( |
| 201 |
!empty($args['xt_plugins_query']) || |
| 202 |
!empty($args['author']) || |
| 203 |
(!empty($args['search']) && !self::search_contains(self::search_terms(), $args['search'])) || |
| 204 |
(!empty($args['tag']) && !self::search_contains(self::search_terms(), $args['tag'])) |
| 205 |
) { |
| 206 |
$res->plugins = self::alter_plugin_results($res->plugins); |
| 207 |
return $res; |
| 208 |
} |
| 209 |
|
| 210 |
return self::get_plugins(); |
| 211 |
} |
| 212 |
|
| 213 |
public static function is_plugin_active($slug): bool |
| 214 |
{ |
| 215 |
|
| 216 |
$slug = str_replace('-lite', '', $slug); |
| 217 |
|
| 218 |
$active_plugins = wp_cache_get('active_plugins'); |
| 219 |
if ($active_plugins === false) { |
| 220 |
$active_plugins = apply_filters('active_plugins', get_option('active_plugins')); |
| 221 |
wp_cache_set('active_plugins', $active_plugins); |
| 222 |
} |
| 223 |
|
| 224 |
foreach ($active_plugins as $plugin) { |
| 225 |
|
| 226 |
if (strpos($plugin, $slug) !== false) { |
| 227 |
return true; |
| 228 |
} |
| 229 |
} |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
public static function alter_plugin_results($plugins): array |
| 234 |
{ |
| 235 |
|
| 236 |
$slugs = array(); |
| 237 |
|
| 238 |
$plugins = array_map(function ($plugin) use (&$slugs) { |
| 239 |
|
| 240 |
if (is_object($plugin)) { |
| 241 |
return $plugin; |
| 242 |
} |
| 243 |
|
| 244 |
if (strpos($plugin['author'], AUTHOR)) { |
| 245 |
|
| 246 |
if (in_array($plugin['slug'], $slugs)) { |
| 247 |
$plugin['duplicated'] = true; |
| 248 |
} |
| 249 |
|
| 250 |
$slugs[] = $plugin['slug']; |
| 251 |
|
| 252 |
return self::alter_plugin_info($plugin); |
| 253 |
} |
| 254 |
|
| 255 |
return $plugin; |
| 256 |
|
| 257 |
}, $plugins); |
| 258 |
|
| 259 |
$plugins = array_filter($plugins, function ($plugin) { |
| 260 |
|
| 261 |
if (is_object($plugin)) { |
| 262 |
return $plugin; |
| 263 |
} |
| 264 |
|
| 265 |
if (!empty($plugin['duplicated'])) { |
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
if (strpos($plugin['author'], AUTHOR) && self::is_plugin_install_page_xt_tabs() && self::is_plugin_active($plugin['slug'])) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
return true; |
| 274 |
}); |
| 275 |
|
| 276 |
return $plugins; |
| 277 |
} |
| 278 |
|
| 279 |
public static function alter_plugin_info($plugin) |
| 280 |
{ |
| 281 |
|
| 282 |
if (!empty($plugin['ratings'])) { |
| 283 |
|
| 284 |
$total_rating_value = 0; |
| 285 |
$ratings = array(); |
| 286 |
|
| 287 |
foreach ($plugin['ratings'] as $rating => $total) { |
| 288 |
|
| 289 |
$total = absint($total); |
| 290 |
|
| 291 |
if (absint($rating) < 4 && $total > 0) { |
| 292 |
$total = 0; |
| 293 |
} |
| 294 |
|
| 295 |
$ratings[$rating] = $total; |
| 296 |
$total_rating_value += ($rating * $total); |
| 297 |
} |
| 298 |
|
| 299 |
$plugin['ratings'] = $ratings; |
| 300 |
if (!empty($plugin['num_ratings'])) { |
| 301 |
$plugin['num_ratings'] = $plugin['ratings']['5'] + $plugin['ratings']['4']; |
| 302 |
$plugin['rating'] = ((($total_rating_value / $plugin['num_ratings']) * 100) / 5); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (!empty($plugin['sections']) && !empty($plugin['sections']['reviews'])) { |
| 307 |
|
| 308 |
$reviews = str_replace('<div class="review">', '|<div class="review">', $plugin['sections']['reviews']); |
| 309 |
$reviews = substr($reviews, 1); |
| 310 |
$reviews = explode('|', $reviews); |
| 311 |
|
| 312 |
$reviews = array_filter($reviews, function ($review) { |
| 313 |
|
| 314 |
preg_match('/data-rating\=\"(4|5)\"/', $review, $match); |
| 315 |
return !empty($match[1]); |
| 316 |
}); |
| 317 |
|
| 318 |
$plugin['sections']['reviews'] = implode('', $reviews); |
| 319 |
} |
| 320 |
|
| 321 |
$plugin['external'] = true; |
| 322 |
$plugin['homepage'] = App::instance()->getExternalUrl('plugin-info', $plugin['homepage']); |
| 323 |
$plugin['author_profile'] = App::instance()->getExternalUrl('plugin-info'); |
| 324 |
$plugin['author'] = sprintf('<a href="%s" target="_blank">%s</a>', $plugin['author_profile'], AUTHOR); |
| 325 |
|
| 326 |
if (!empty($plugin['contributors'])) { |
| 327 |
foreach ($plugin['contributors'] as $key => $contributor) { |
| 328 |
|
| 329 |
if ($key === strtolower(AUTHOR)) { |
| 330 |
|
| 331 |
$contributor['profile'] = $plugin['author_profile']; |
| 332 |
$contributor['display_name'] = AUTHOR; |
| 333 |
$plugin['contributors'][$key] = $contributor; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
return $plugin; |
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
|