| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class that exposes hooks for recommend. |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Rollback |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ThemeisleSDK\Modules; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
use ThemeisleSDK\Common\Abstract_Module; |
| 16 |
use ThemeisleSDK\Product; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Expose endpoints for ThemeIsle SDK. |
| 24 |
*/ |
| 25 |
class Recommendation extends Abstract_Module { |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Load module logic. |
| 30 |
* |
| 31 |
* @param Product $product Product to load. |
| 32 |
*/ |
| 33 |
public function load( $product ) { |
| 34 |
$this->product = $product; |
| 35 |
$this->setup_hooks(); |
| 36 |
|
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Setup endpoints. |
| 42 |
*/ |
| 43 |
private function setup_hooks() { |
| 44 |
add_action( $this->product->get_key() . '_recommend_products', array( $this, 'render_products_box' ), 10, 4 ); |
| 45 |
add_action( 'admin_head', array( $this, 'enqueue' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Check if we should load the module for this product. |
| 50 |
* |
| 51 |
* @param Product $product Product data. |
| 52 |
* |
| 53 |
* @return bool Should we load the module? |
| 54 |
*/ |
| 55 |
public function can_load( $product ) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Render products box content. |
| 61 |
* |
| 62 |
* @param array $plugins_list - list of useful plugins (in slug => nicename format). |
| 63 |
* @param array $themes_list - list of useful themes (in slug => nicename format). |
| 64 |
* @param array $strings - list of translated strings. |
| 65 |
* @param array $preferences - list of preferences. |
| 66 |
*/ |
| 67 |
public function render_products_box( $plugins_list, $themes_list, $strings, $preferences = array() ) { |
| 68 |
|
| 69 |
if ( empty( $plugins_list ) && empty( $themes_list ) ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! empty( $plugins_list ) && ! current_user_can( 'install_plugins' ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if ( ! empty( $themes_list ) && ! current_user_can( 'install_themes' ) ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
add_thickbox(); |
| 82 |
|
| 83 |
if ( ! empty( $themes_list ) ) { |
| 84 |
$list = $this->get_themes( $themes_list, $preferences ); |
| 85 |
|
| 86 |
if ( has_action( $this->product->get_key() . '_recommend_products_theme_template' ) ) { |
| 87 |
do_action( $this->product->get_key() . '_recommend_products_theme_template', $list, $strings, $preferences ); |
| 88 |
} else { |
| 89 |
echo '<div class="recommend-product">'; |
| 90 |
|
| 91 |
foreach ( $list as $theme ) { |
| 92 |
echo '<div class="plugin_box">'; |
| 93 |
echo ' <img class="theme-banner" src="' . esc_url( $theme->screenshot_url ) . '">'; |
| 94 |
echo ' <div class="title-action-wrapper">'; |
| 95 |
echo ' <span class="plugin-name">' . esc_html( $theme->custom_name ) . '</span>'; |
| 96 |
if ( ! isset( $preferences['description'] ) || ( isset( $preferences['description'] ) && $preferences['description'] ) ) { |
| 97 |
echo '<span class="plugin-desc">' . esc_html( substr( $theme->description, 0, strpos( $theme->description, '.' ) ) ) . '.</span>'; |
| 98 |
} |
| 99 |
echo ' </div>'; |
| 100 |
echo '<div class="plugin-box-footer">'; |
| 101 |
echo ' <div class="button-wrap">'; |
| 102 |
echo ' <a class="button button-primary " href="' . esc_url( $theme->custom_url ) . '"><span class="dashicons dashicons-external"></span>' . esc_html( $strings['install'] ) . '</a>'; |
| 103 |
echo ' </div>'; |
| 104 |
echo ' </div>'; |
| 105 |
echo '</div>'; |
| 106 |
} |
| 107 |
|
| 108 |
echo '</div>'; |
| 109 |
} |
| 110 |
} |
| 111 |
if ( ! empty( $plugins_list ) ) { |
| 112 |
$list = $this->get_plugins( $plugins_list, $preferences ); |
| 113 |
|
| 114 |
if ( has_action( $this->product->get_key() . '_recommend_products_plugin_template' ) ) { |
| 115 |
do_action( $this->product->get_key() . '_recommend_products_plugin_template', $list, $strings, $preferences ); |
| 116 |
} else { |
| 117 |
echo '<div class="recommend-product">'; |
| 118 |
|
| 119 |
foreach ( $list as $current_plugin ) { |
| 120 |
echo '<div class="plugin_box">'; |
| 121 |
echo ' <img class="plugin-banner" src="' . esc_url( $current_plugin->custom_image ) . '">'; |
| 122 |
echo ' <div class="title-action-wrapper">'; |
| 123 |
echo ' <span class="plugin-name">' . esc_html( $current_plugin->custom_name ) . '</span>'; |
| 124 |
if ( ! isset( $preferences['description'] ) || ( isset( $preferences['description'] ) && $preferences['description'] ) ) { |
| 125 |
echo '<span class="plugin-desc">' . esc_html( substr( $current_plugin->short_description, 0, strpos( $current_plugin->short_description, '.' ) ) ) . '. </span>'; |
| 126 |
} |
| 127 |
echo ' </div>'; |
| 128 |
echo ' <div class="plugin-box-footer">'; |
| 129 |
echo ' <a class="button button-primary thickbox open-plugin-details-modal" href="' . esc_url( $current_plugin->custom_url ) . '"><span class="dashicons dashicons-external"></span>' . esc_html( $strings['install'] ) . '</a>'; |
| 130 |
echo ' </div>'; |
| 131 |
echo '</div>'; |
| 132 |
} |
| 133 |
|
| 134 |
echo '</div>'; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Collect all the information for the themes list. |
| 142 |
* |
| 143 |
* @param array $themes_list - list of useful themes (in slug => nicename format). |
| 144 |
* @param array $preferences - list of preferences. |
| 145 |
* |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
private function get_themes( $themes_list, $preferences ) { |
| 149 |
$list = array(); |
| 150 |
foreach ( $themes_list as $slug => $nicename ) { |
| 151 |
$theme = $this->call_theme_api( $slug ); |
| 152 |
if ( ! $theme ) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
$url = add_query_arg( |
| 157 |
array( |
| 158 |
'theme' => $theme->slug, |
| 159 |
), |
| 160 |
network_admin_url( 'theme-install.php' ) |
| 161 |
); |
| 162 |
|
| 163 |
$name = empty( $nicename ) ? $theme->name : $nicename; |
| 164 |
|
| 165 |
$theme->custom_url = $url; |
| 166 |
$theme->custom_name = $name; |
| 167 |
|
| 168 |
$list[] = $theme; |
| 169 |
} |
| 170 |
|
| 171 |
return $list; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Call theme api |
| 176 |
* |
| 177 |
* @param string $slug theme slug. |
| 178 |
* |
| 179 |
* @return array|mixed|object |
| 180 |
*/ |
| 181 |
private function call_theme_api( $slug ) { |
| 182 |
$theme = get_transient( 'ti_theme_info_' . $slug ); |
| 183 |
|
| 184 |
if ( false !== $theme ) { |
| 185 |
return $theme; |
| 186 |
} |
| 187 |
|
| 188 |
$products = $this->safe_get( |
| 189 |
'https://api.wordpress.org/themes/info/1.1/?action=query_themes&request[theme]=' . $slug . '&request[per_page]=1' |
| 190 |
); |
| 191 |
$products = json_decode( wp_remote_retrieve_body( $products ) ); |
| 192 |
if ( is_object( $products ) ) { |
| 193 |
$theme = $products->themes[0]; |
| 194 |
set_transient( 'ti_theme_info_' . $slug, $theme, 6 * HOUR_IN_SECONDS ); |
| 195 |
} |
| 196 |
|
| 197 |
return $theme; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Collect all the information for the plugins list. |
| 202 |
* |
| 203 |
* @param array $plugins_list - list of useful plugins (in slug => nicename format). |
| 204 |
* @param array $preferences - list of preferences. |
| 205 |
* |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
private function get_plugins( $plugins_list, $preferences ) { |
| 209 |
$list = array(); |
| 210 |
foreach ( $plugins_list as $plugin => $nicename ) { |
| 211 |
$current_plugin = $this->call_plugin_api( $plugin ); |
| 212 |
|
| 213 |
$name = empty( $nicename ) ? $current_plugin->name : $nicename; |
| 214 |
|
| 215 |
$image = $current_plugin->banners['low']; |
| 216 |
if ( isset( $preferences['image'] ) && 'icon' === $preferences['image'] ) { |
| 217 |
$image = $current_plugin->icons['1x']; |
| 218 |
} |
| 219 |
|
| 220 |
$url = add_query_arg( |
| 221 |
array( |
| 222 |
'tab' => 'plugin-information', |
| 223 |
'plugin' => $current_plugin->slug, |
| 224 |
'TB_iframe' => true, |
| 225 |
'width' => 800, |
| 226 |
'height' => 800, |
| 227 |
), |
| 228 |
network_admin_url( 'plugin-install.php' ) |
| 229 |
); |
| 230 |
|
| 231 |
$current_plugin->custom_url = $url; |
| 232 |
$current_plugin->custom_name = $name; |
| 233 |
$current_plugin->custom_image = $image; |
| 234 |
|
| 235 |
$list[] = $current_plugin; |
| 236 |
} |
| 237 |
|
| 238 |
return $list; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Call plugin api |
| 243 |
* |
| 244 |
* @param string $slug plugin slug. |
| 245 |
* |
| 246 |
* @return array|mixed|object |
| 247 |
*/ |
| 248 |
private function call_plugin_api( $slug ) { |
| 249 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 250 |
|
| 251 |
$call_api = get_transient( 'ti_plugin_info_' . $slug ); |
| 252 |
|
| 253 |
if ( false === $call_api ) { |
| 254 |
$call_api = plugins_api( |
| 255 |
'plugin_information', |
| 256 |
array( |
| 257 |
'slug' => $slug, |
| 258 |
'fields' => array( |
| 259 |
'downloaded' => false, |
| 260 |
'rating' => false, |
| 261 |
'description' => false, |
| 262 |
'short_description' => true, |
| 263 |
'donate_link' => false, |
| 264 |
'tags' => false, |
| 265 |
'sections' => true, |
| 266 |
'homepage' => true, |
| 267 |
'added' => false, |
| 268 |
'last_updated' => false, |
| 269 |
'compatibility' => false, |
| 270 |
'tested' => false, |
| 271 |
'requires' => false, |
| 272 |
'downloadlink' => false, |
| 273 |
'icons' => true, |
| 274 |
'banners' => true, |
| 275 |
), |
| 276 |
) |
| 277 |
); |
| 278 |
set_transient( 'ti_plugin_info_' . $slug, $call_api, 30 * MINUTE_IN_SECONDS ); |
| 279 |
} |
| 280 |
|
| 281 |
return $call_api; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Load css and scripts for the plugin recommend page. |
| 286 |
*/ |
| 287 |
public function enqueue() { |
| 288 |
$screen = get_current_screen(); |
| 289 |
|
| 290 |
if ( ! isset( $screen->id ) ) { |
| 291 |
return; |
| 292 |
} |
| 293 |
if ( false === apply_filters( $this->product->get_key() . '_enqueue_recommend', false, $screen->id ) ) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
?> |
| 298 |
<style type="text/css"> |
| 299 |
.recommend-product { |
| 300 |
display: flex; |
| 301 |
justify-content: space-between; |
| 302 |
flex-wrap: wrap; |
| 303 |
} |
| 304 |
|
| 305 |
.recommend-product .theme-banner { |
| 306 |
width: 200px; |
| 307 |
margin: auto; |
| 308 |
} |
| 309 |
|
| 310 |
.recommend-product .plugin-banner { |
| 311 |
width: 100px; |
| 312 |
margin: auto; |
| 313 |
} |
| 314 |
|
| 315 |
.recommend-product .plugin_box .button span { |
| 316 |
|
| 317 |
margin-top: 2px; |
| 318 |
margin-right: 7px; |
| 319 |
} |
| 320 |
|
| 321 |
.recommend-product .plugin_box .button { |
| 322 |
margin-bottom: 10px; |
| 323 |
} |
| 324 |
|
| 325 |
.recommend-product .plugin_box { |
| 326 |
margin-bottom: 20px; |
| 327 |
padding-top: 5px; |
| 328 |
display: flex; |
| 329 |
box-shadow: 0px 0px 10px -5px rgba(0, 0, 0, 0.55); |
| 330 |
background: #fff; |
| 331 |
border-radius: 5px; |
| 332 |
flex-direction: column; |
| 333 |
justify-content: flex-start; |
| 334 |
width: 95%; |
| 335 |
} |
| 336 |
|
| 337 |
.recommend-product .title-action-wrapper { |
| 338 |
padding: 15px 20px 5px 20px; |
| 339 |
} |
| 340 |
|
| 341 |
.recommend-product .plugin-name { |
| 342 |
font-size: 18px; |
| 343 |
display: block; |
| 344 |
white-space: nowrap; |
| 345 |
text-overflow: ellipsis; |
| 346 |
margin-bottom: 10px; |
| 347 |
overflow: hidden; |
| 348 |
line-height: normal; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
.recommend-product .plugin-desc { |
| 353 |
display: block; |
| 354 |
margin-bottom: 10px; |
| 355 |
font-size: 13px; |
| 356 |
color: #777; |
| 357 |
line-height: 1.6; |
| 358 |
} |
| 359 |
|
| 360 |
.recommend-product .button-wrap > div { |
| 361 |
padding: 0; |
| 362 |
margin: 0; |
| 363 |
} |
| 364 |
|
| 365 |
.plugin-box-footer { |
| 366 |
display: flex; |
| 367 |
justify-content: space-around; |
| 368 |
vertical-align: middle; |
| 369 |
align-items: center; |
| 370 |
padding: 0px 10px 5px; |
| 371 |
flex: 1; |
| 372 |
margin-top: auto; |
| 373 |
} |
| 374 |
</style> |
| 375 |
<?php |
| 376 |
} |
| 377 |
} |
| 378 |
|