templates-patterns-collection
/
vendor
/
codeinwp
/
themeisle-sdk
/
src
/
Modules
/
Recommendation.php
About_us.php
3 weeks ago
Abstract_Migration.php
2 months ago
Announcements.php
2 months ago
Compatibilities.php
2 years ago
Dashboard_widget.php
3 weeks ago
Featured_plugins.php
1 month ago
Float_widget.php
1 year ago
Licenser.php
2 months ago
Logger.php
10 months ago
Migrator.php
2 months ago
Notification.php
3 years ago
Promotions.php
3 weeks ago
Recommendation.php
3 years ago
Review.php
1 year ago
Rollback.php
1 year ago
Script_loader.php
1 year ago
Translate.php
5 years ago
Translations.php
1 year ago
Uninstall_feedback.php
2 days ago
Welcome.php
2 years ago
Recommendation.php
335 lines
| 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 | * Load css and scripts for the plugin recommend page. |
| 243 | */ |
| 244 | public function enqueue() { |
| 245 | $screen = get_current_screen(); |
| 246 | |
| 247 | if ( ! isset( $screen->id ) ) { |
| 248 | return; |
| 249 | } |
| 250 | if ( false === apply_filters( $this->product->get_key() . '_enqueue_recommend', false, $screen->id ) ) { |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | ?> |
| 255 | <style type="text/css"> |
| 256 | .recommend-product { |
| 257 | display: flex; |
| 258 | justify-content: space-between; |
| 259 | flex-wrap: wrap; |
| 260 | } |
| 261 | |
| 262 | .recommend-product .theme-banner { |
| 263 | width: 200px; |
| 264 | margin: auto; |
| 265 | } |
| 266 | |
| 267 | .recommend-product .plugin-banner { |
| 268 | width: 100px; |
| 269 | margin: auto; |
| 270 | } |
| 271 | |
| 272 | .recommend-product .plugin_box .button span { |
| 273 | |
| 274 | margin-top: 2px; |
| 275 | margin-right: 7px; |
| 276 | } |
| 277 | |
| 278 | .recommend-product .plugin_box .button { |
| 279 | margin-bottom: 10px; |
| 280 | } |
| 281 | |
| 282 | .recommend-product .plugin_box { |
| 283 | margin-bottom: 20px; |
| 284 | padding-top: 5px; |
| 285 | display: flex; |
| 286 | box-shadow: 0px 0px 10px -5px rgba(0, 0, 0, 0.55); |
| 287 | background: #fff; |
| 288 | border-radius: 5px; |
| 289 | flex-direction: column; |
| 290 | justify-content: flex-start; |
| 291 | width: 95%; |
| 292 | } |
| 293 | |
| 294 | .recommend-product .title-action-wrapper { |
| 295 | padding: 15px 20px 5px 20px; |
| 296 | } |
| 297 | |
| 298 | .recommend-product .plugin-name { |
| 299 | font-size: 18px; |
| 300 | display: block; |
| 301 | white-space: nowrap; |
| 302 | text-overflow: ellipsis; |
| 303 | margin-bottom: 10px; |
| 304 | overflow: hidden; |
| 305 | line-height: normal; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | .recommend-product .plugin-desc { |
| 310 | display: block; |
| 311 | margin-bottom: 10px; |
| 312 | font-size: 13px; |
| 313 | color: #777; |
| 314 | line-height: 1.6; |
| 315 | } |
| 316 | |
| 317 | .recommend-product .button-wrap > div { |
| 318 | padding: 0; |
| 319 | margin: 0; |
| 320 | } |
| 321 | |
| 322 | .plugin-box-footer { |
| 323 | display: flex; |
| 324 | justify-content: space-around; |
| 325 | vertical-align: middle; |
| 326 | align-items: center; |
| 327 | padding: 0px 10px 5px; |
| 328 | flex: 1; |
| 329 | margin-top: auto; |
| 330 | } |
| 331 | </style> |
| 332 | <?php |
| 333 | } |
| 334 | } |
| 335 |