helper
5 years ago
importers
5 years ago
list-tables
5 years ago
marketplace-suggestions
5 years ago
meta-boxes
5 years ago
notes
5 years ago
plugin-updates
5 years ago
reports
5 years ago
settings
5 years ago
views
5 years ago
class-wc-admin-addons.php
5 years ago
class-wc-admin-api-keys-table-list.php
6 years ago
class-wc-admin-api-keys.php
6 years ago
class-wc-admin-assets.php
5 years ago
class-wc-admin-attributes.php
5 years ago
class-wc-admin-customize.php
5 years ago
class-wc-admin-dashboard.php
5 years ago
class-wc-admin-duplicate-product.php
5 years ago
class-wc-admin-exporters.php
5 years ago
class-wc-admin-help.php
5 years ago
class-wc-admin-importers.php
5 years ago
class-wc-admin-log-table-list.php
5 years ago
class-wc-admin-menus.php
5 years ago
class-wc-admin-meta-boxes.php
5 years ago
class-wc-admin-notices.php
5 years ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
5 years ago
class-wc-admin-post-types.php
5 years ago
class-wc-admin-profile.php
5 years ago
class-wc-admin-reports.php
5 years ago
class-wc-admin-settings.php
5 years ago
class-wc-admin-setup-wizard.php
5 years ago
class-wc-admin-status.php
5 years ago
class-wc-admin-taxonomies.php
5 years ago
class-wc-admin-webhooks-table-list.php
7 years ago
class-wc-admin-webhooks.php
5 years ago
class-wc-admin.php
5 years ago
wc-admin-functions.php
5 years ago
wc-meta-box-functions.php
5 years ago
class-wc-admin-addons.php
712 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Addons Page |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 2.5.0 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Constants; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * WC_Admin_Addons Class. |
| 17 | */ |
| 18 | class WC_Admin_Addons { |
| 19 | |
| 20 | /** |
| 21 | * Get featured for the addons screen |
| 22 | * |
| 23 | * @return array of objects |
| 24 | */ |
| 25 | public static function get_featured() { |
| 26 | $featured = get_transient( 'wc_addons_featured' ); |
| 27 | if ( false === $featured ) { |
| 28 | $raw_featured = wp_safe_remote_get( 'https://d3t0oesq8995hv.cloudfront.net/add-ons/featured-v2.json', array( 'user-agent' => 'WooCommerce Addons Page' ) ); |
| 29 | if ( ! is_wp_error( $raw_featured ) ) { |
| 30 | $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); |
| 31 | if ( $featured ) { |
| 32 | set_transient( 'wc_addons_featured', $featured, WEEK_IN_SECONDS ); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | if ( is_object( $featured ) ) { |
| 38 | self::output_featured_sections( $featured->sections ); |
| 39 | return $featured; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Build url parameter string |
| 45 | * |
| 46 | * @param string $category Addon (sub) category. |
| 47 | * @param string $term Search terms. |
| 48 | * @param string $country Store country. |
| 49 | * |
| 50 | * @return string url parameter string |
| 51 | */ |
| 52 | public static function build_parameter_string( $category, $term, $country ) { |
| 53 | |
| 54 | $parameters = array( |
| 55 | 'category' => $category, |
| 56 | 'term' => $term, |
| 57 | 'country' => $country, |
| 58 | ); |
| 59 | |
| 60 | return '?' . http_build_query( $parameters ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Call API to get extensions |
| 65 | * |
| 66 | * @param string $category Addon (sub) category. |
| 67 | * @param string $term Search terms. |
| 68 | * @param string $country Store country. |
| 69 | * |
| 70 | * @return array of extensions |
| 71 | */ |
| 72 | public static function get_extension_data( $category, $term, $country ) { |
| 73 | $parameters = self::build_parameter_string( $category, $term, $country ); |
| 74 | $raw_extensions = wp_remote_get( |
| 75 | 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters |
| 76 | ); |
| 77 | if ( ! is_wp_error( $raw_extensions ) ) { |
| 78 | $addons = json_decode( wp_remote_retrieve_body( $raw_extensions ) )->products; |
| 79 | } |
| 80 | return $addons; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get sections for the addons screen |
| 85 | * |
| 86 | * @return array of objects |
| 87 | */ |
| 88 | public static function get_sections() { |
| 89 | $addon_sections = get_transient( 'wc_addons_sections' ); |
| 90 | if ( false === ( $addon_sections ) ) { |
| 91 | $raw_sections = wp_safe_remote_get( |
| 92 | 'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories' |
| 93 | ); |
| 94 | if ( ! is_wp_error( $raw_sections ) ) { |
| 95 | $addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) ); |
| 96 | if ( $addon_sections ) { |
| 97 | set_transient( 'wc_addons_sections', $addon_sections, WEEK_IN_SECONDS ); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | return apply_filters( 'woocommerce_addons_sections', $addon_sections ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get section for the addons screen. |
| 106 | * |
| 107 | * @param string $section_id Required section ID. |
| 108 | * |
| 109 | * @return object|bool |
| 110 | */ |
| 111 | public static function get_section( $section_id ) { |
| 112 | $sections = self::get_sections(); |
| 113 | if ( isset( $sections[ $section_id ] ) ) { |
| 114 | return $sections[ $section_id ]; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get section content for the addons screen. |
| 121 | * |
| 122 | * @param string $section_id Required section ID. |
| 123 | * |
| 124 | * @return array |
| 125 | */ |
| 126 | public static function get_section_data( $section_id ) { |
| 127 | $section = self::get_section( $section_id ); |
| 128 | $section_data = ''; |
| 129 | |
| 130 | if ( ! empty( $section->endpoint ) ) { |
| 131 | $section_data = get_transient( 'wc_addons_section_' . $section_id ); |
| 132 | if ( false === $section_data ) { |
| 133 | $raw_section = wp_safe_remote_get( esc_url_raw( $section->endpoint ), array( 'user-agent' => 'WooCommerce Addons Page' ) ); |
| 134 | |
| 135 | if ( ! is_wp_error( $raw_section ) ) { |
| 136 | $section_data = json_decode( wp_remote_retrieve_body( $raw_section ) ); |
| 137 | |
| 138 | if ( ! empty( $section_data->products ) ) { |
| 139 | set_transient( 'wc_addons_section_' . $section_id, $section_data, WEEK_IN_SECONDS ); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return apply_filters( 'woocommerce_addons_section_data', $section_data->products, $section_id ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Handles the outputting of a contextually aware Storefront link (points to child themes if Storefront is already active). |
| 150 | */ |
| 151 | public static function output_storefront_button() { |
| 152 | $template = get_option( 'template' ); |
| 153 | $stylesheet = get_option( 'stylesheet' ); |
| 154 | |
| 155 | if ( 'storefront' === $template ) { |
| 156 | if ( 'storefront' === $stylesheet ) { |
| 157 | $url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/'; |
| 158 | $text = __( 'Need a fresh look? Try Storefront child themes', 'woocommerce' ); |
| 159 | $utm_content = 'nostorefrontchildtheme'; |
| 160 | } else { |
| 161 | $url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/'; |
| 162 | $text = __( 'View more Storefront child themes', 'woocommerce' ); |
| 163 | $utm_content = 'hasstorefrontchildtheme'; |
| 164 | } |
| 165 | } else { |
| 166 | $url = 'https://woocommerce.com/storefront/'; |
| 167 | $text = __( 'Need a theme? Try Storefront', 'woocommerce' ); |
| 168 | $utm_content = 'nostorefront'; |
| 169 | } |
| 170 | |
| 171 | $url = add_query_arg( |
| 172 | array( |
| 173 | 'utm_source' => 'addons', |
| 174 | 'utm_medium' => 'product', |
| 175 | 'utm_campaign' => 'woocommerceplugin', |
| 176 | 'utm_content' => $utm_content, |
| 177 | ), |
| 178 | $url |
| 179 | ); |
| 180 | |
| 181 | echo '<a href="' . esc_url( $url ) . '" class="add-new-h2">' . esc_html( $text ) . '</a>' . "\n"; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Handles the outputting of a banner block. |
| 186 | * |
| 187 | * @param object $block Banner data. |
| 188 | */ |
| 189 | public static function output_banner_block( $block ) { |
| 190 | ?> |
| 191 | <div class="addons-banner-block"> |
| 192 | <h1><?php echo esc_html( $block->title ); ?></h1> |
| 193 | <p><?php echo esc_html( $block->description ); ?></p> |
| 194 | <div class="addons-banner-block-items"> |
| 195 | <?php foreach ( $block->items as $item ) : ?> |
| 196 | <?php if ( self::show_extension( $item ) ) : ?> |
| 197 | <div class="addons-banner-block-item"> |
| 198 | <div class="addons-banner-block-item-icon"> |
| 199 | <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" /> |
| 200 | </div> |
| 201 | <div class="addons-banner-block-item-content"> |
| 202 | <h3><?php echo esc_html( $item->title ); ?></h3> |
| 203 | <p><?php echo esc_html( $item->description ); ?></p> |
| 204 | <?php |
| 205 | self::output_button( |
| 206 | $item->href, |
| 207 | $item->button, |
| 208 | 'addons-button-solid', |
| 209 | $item->plugin |
| 210 | ); |
| 211 | ?> |
| 212 | </div> |
| 213 | </div> |
| 214 | <?php endif; ?> |
| 215 | <?php endforeach; ?> |
| 216 | </div> |
| 217 | </div> |
| 218 | <?php |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Handles the outputting of a column. |
| 223 | * |
| 224 | * @param object $block Column data. |
| 225 | */ |
| 226 | public static function output_column( $block ) { |
| 227 | if ( isset( $block->container ) && 'column_container_start' === $block->container ) { |
| 228 | ?> |
| 229 | <div class="addons-column-section"> |
| 230 | <?php |
| 231 | } |
| 232 | if ( 'column_start' === $block->module ) { |
| 233 | ?> |
| 234 | <div class="addons-column"> |
| 235 | <?php |
| 236 | } else { |
| 237 | ?> |
| 238 | </div> |
| 239 | <?php |
| 240 | } |
| 241 | if ( isset( $block->container ) && 'column_container_end' === $block->container ) { |
| 242 | ?> |
| 243 | </div> |
| 244 | <?php |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Handles the outputting of a column block. |
| 250 | * |
| 251 | * @param object $block Column block data. |
| 252 | */ |
| 253 | public static function output_column_block( $block ) { |
| 254 | ?> |
| 255 | <div class="addons-column-block"> |
| 256 | <h1><?php echo esc_html( $block->title ); ?></h1> |
| 257 | <p><?php echo esc_html( $block->description ); ?></p> |
| 258 | <?php foreach ( $block->items as $item ) : ?> |
| 259 | <?php if ( self::show_extension( $item ) ) : ?> |
| 260 | <div class="addons-column-block-item"> |
| 261 | <div class="addons-column-block-item-icon"> |
| 262 | <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" /> |
| 263 | </div> |
| 264 | <div class="addons-column-block-item-content"> |
| 265 | <h2><?php echo esc_html( $item->title ); ?></h2> |
| 266 | <?php |
| 267 | self::output_button( |
| 268 | $item->href, |
| 269 | $item->button, |
| 270 | 'addons-button-solid', |
| 271 | $item->plugin |
| 272 | ); |
| 273 | ?> |
| 274 | <p><?php echo esc_html( $item->description ); ?></p> |
| 275 | </div> |
| 276 | </div> |
| 277 | <?php endif; ?> |
| 278 | <?php endforeach; ?> |
| 279 | </div> |
| 280 | |
| 281 | <?php |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Handles the outputting of a small light block. |
| 286 | * |
| 287 | * @param object $block Block data. |
| 288 | */ |
| 289 | public static function output_small_light_block( $block ) { |
| 290 | ?> |
| 291 | <div class="addons-small-light-block"> |
| 292 | <img class="addons-img" src="<?php echo esc_url( $block->image ); ?>" /> |
| 293 | <div class="addons-small-light-block-content"> |
| 294 | <h1><?php echo esc_html( $block->title ); ?></h1> |
| 295 | <p><?php echo esc_html( $block->description ); ?></p> |
| 296 | <div class="addons-small-light-block-buttons"> |
| 297 | <?php foreach ( $block->buttons as $button ) : ?> |
| 298 | <?php |
| 299 | self::output_button( |
| 300 | $button->href, |
| 301 | $button->text, |
| 302 | 'addons-button-solid' |
| 303 | ); |
| 304 | ?> |
| 305 | <?php endforeach; ?> |
| 306 | </div> |
| 307 | </div> |
| 308 | </div> |
| 309 | <?php |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Handles the outputting of a small dark block. |
| 314 | * |
| 315 | * @param object $block Block data. |
| 316 | */ |
| 317 | public static function output_small_dark_block( $block ) { |
| 318 | ?> |
| 319 | <div class="addons-small-dark-block"> |
| 320 | <h1><?php echo esc_html( $block->title ); ?></h1> |
| 321 | <p><?php echo esc_html( $block->description ); ?></p> |
| 322 | <div class="addons-small-dark-items"> |
| 323 | <?php foreach ( $block->items as $item ) : ?> |
| 324 | <div class="addons-small-dark-item"> |
| 325 | <?php if ( ! empty( $item->image ) ) : ?> |
| 326 | <div class="addons-small-dark-item-icon"> |
| 327 | <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" /> |
| 328 | </div> |
| 329 | <?php endif; ?> |
| 330 | <?php |
| 331 | self::output_button( |
| 332 | $item->href, |
| 333 | $item->button, |
| 334 | 'addons-button-outline-white' |
| 335 | ); |
| 336 | ?> |
| 337 | </div> |
| 338 | <?php endforeach; ?> |
| 339 | </div> |
| 340 | </div> |
| 341 | <?php |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Handles the outputting of the WooCommerce Services banner block. |
| 346 | * |
| 347 | * @param object $block Block data. |
| 348 | */ |
| 349 | public static function output_wcs_banner_block( $block = array() ) { |
| 350 | $is_active = is_plugin_active( 'woocommerce-services/woocommerce-services.php' ); |
| 351 | $location = wc_get_base_location(); |
| 352 | |
| 353 | if ( |
| 354 | ! in_array( $location['country'], array( 'US', 'CA' ), true ) || |
| 355 | $is_active || |
| 356 | ! current_user_can( 'install_plugins' ) || |
| 357 | ! current_user_can( 'activate_plugins' ) |
| 358 | ) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | $button_url = wp_nonce_url( |
| 363 | add_query_arg( |
| 364 | array( |
| 365 | 'install-addon' => 'woocommerce-services', |
| 366 | ) |
| 367 | ), |
| 368 | 'install-addon_woocommerce-services' |
| 369 | ); |
| 370 | |
| 371 | $defaults = array( |
| 372 | 'image' => WC()->plugin_url() . '/assets/images/wcs-extensions-banner-3x.png', |
| 373 | 'image_alt' => __( 'WooCommerce Shipping', 'woocommerce' ), |
| 374 | 'title' => __( 'Buy discounted shipping labels — then print them from your dashboard.', 'woocommerce' ), |
| 375 | 'description' => __( 'Integrate your store with USPS to buy discounted shipping labels, and print them directly from your WooCommerce dashboard. Powered by WooCommerce Shipping.', 'woocommerce' ), |
| 376 | 'button' => __( 'Free - Install now', 'woocommerce' ), |
| 377 | 'href' => $button_url, |
| 378 | 'logos' => array(), |
| 379 | ); |
| 380 | |
| 381 | switch ( $location['country'] ) { |
| 382 | case 'CA': |
| 383 | $local_defaults = array( |
| 384 | 'image' => WC()->plugin_url() . '/assets/images/wcs-truck-banner-3x.png', |
| 385 | 'title' => __( 'Show Canada Post shipping rates', 'woocommerce' ), |
| 386 | 'description' => __( 'Display live rates from Canada Post at checkout to make shipping a breeze. Powered by WooCommerce Shipping.', 'woocommerce' ), |
| 387 | 'logos' => array_merge( |
| 388 | $defaults['logos'], |
| 389 | array( |
| 390 | array( |
| 391 | 'link' => WC()->plugin_url() . '/assets/images/wcs-canada-post-logo.jpg', |
| 392 | 'alt' => 'Canada Post logo', |
| 393 | ), |
| 394 | ) |
| 395 | ), |
| 396 | ); |
| 397 | break; |
| 398 | case 'US': |
| 399 | $local_defaults = array( |
| 400 | 'logos' => array_merge( |
| 401 | $defaults['logos'], |
| 402 | array( |
| 403 | array( |
| 404 | 'link' => WC()->plugin_url() . '/assets/images/wcs-usps-logo.png', |
| 405 | 'alt' => 'USPS logo', |
| 406 | ), |
| 407 | ) |
| 408 | ), |
| 409 | ); |
| 410 | break; |
| 411 | default: |
| 412 | $local_defaults = array(); |
| 413 | } |
| 414 | |
| 415 | $block_data = array_merge( $defaults, $local_defaults, $block ); |
| 416 | ?> |
| 417 | <div class="addons-wcs-banner-block"> |
| 418 | <div class="addons-wcs-banner-block-image"> |
| 419 | <img |
| 420 | class="addons-img" |
| 421 | src="<?php echo esc_url( $block_data['image'] ); ?>" |
| 422 | alt="<?php echo esc_attr( $block_data['image_alt'] ); ?>" |
| 423 | /> |
| 424 | </div> |
| 425 | <div class="addons-wcs-banner-block-content"> |
| 426 | <h1><?php echo esc_html( $block_data['title'] ); ?></h1> |
| 427 | <p><?php echo esc_html( $block_data['description'] ); ?></p> |
| 428 | <ul> |
| 429 | <?php foreach ( $block_data['logos'] as $logo ) : ?> |
| 430 | <li> |
| 431 | <img |
| 432 | alt="<?php echo esc_attr( $logo['alt'] ); ?>" |
| 433 | class="wcs-service-logo" |
| 434 | src="<?php echo esc_url( $logo['link'] ); ?>" |
| 435 | > |
| 436 | </li> |
| 437 | <?php endforeach; ?> |
| 438 | </ul> |
| 439 | <?php |
| 440 | self::output_button( |
| 441 | $block_data['href'], |
| 442 | $block_data['button'], |
| 443 | 'addons-button-outline-purple' |
| 444 | ); |
| 445 | ?> |
| 446 | </div> |
| 447 | </div> |
| 448 | <?php |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Handles the outputting of the WooCommerce Pay banner block. |
| 453 | * |
| 454 | * @param object $block Block data. |
| 455 | */ |
| 456 | public static function output_wcpay_banner_block( $block = array() ) { |
| 457 | $is_active = is_plugin_active( 'woocommerce-payments/woocommerce-payments.php' ); |
| 458 | $location = wc_get_base_location(); |
| 459 | |
| 460 | if ( |
| 461 | ! in_array( $location['country'], array( 'US' ), true ) || |
| 462 | $is_active || |
| 463 | ! current_user_can( 'install_plugins' ) || |
| 464 | ! current_user_can( 'activate_plugins' ) |
| 465 | ) { |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | $button_url = wp_nonce_url( |
| 470 | add_query_arg( |
| 471 | array( |
| 472 | 'install-addon' => 'woocommerce-payments', |
| 473 | ) |
| 474 | ), |
| 475 | 'install-addon_woocommerce-payments' |
| 476 | ); |
| 477 | |
| 478 | $defaults = array( |
| 479 | 'image' => WC()->plugin_url() . '/assets/images/wcpayments-icon-secure.png', |
| 480 | 'image_alt' => __( 'WooCommerce Payments', 'woocommerce' ), |
| 481 | 'title' => __( 'Payments made simple, with no monthly fees — exclusively for WooCommerce stores.', 'woocommerce' ), |
| 482 | 'description' => __( 'Securely accept cards in your store. See payments, track cash flow into your bank account, and stay on top of disputes – right from your dashboard.', 'woocommerce' ), |
| 483 | 'button' => __( 'Free - Install now', 'woocommerce' ), |
| 484 | 'href' => $button_url, |
| 485 | 'logos' => array(), |
| 486 | ); |
| 487 | |
| 488 | $block_data = array_merge( $defaults, $block ); |
| 489 | ?> |
| 490 | <div class="addons-wcs-banner-block"> |
| 491 | <div class="addons-wcs-banner-block-image"> |
| 492 | <img |
| 493 | class="addons-img" |
| 494 | src="<?php echo esc_url( $block_data['image'] ); ?>" |
| 495 | alt="<?php echo esc_attr( $block_data['image_alt'] ); ?>" |
| 496 | /> |
| 497 | </div> |
| 498 | <div class="addons-wcs-banner-block-content"> |
| 499 | <h1><?php echo esc_html( $block_data['title'] ); ?></h1> |
| 500 | <p><?php echo esc_html( $block_data['description'] ); ?></p> |
| 501 | <?php |
| 502 | self::output_button( |
| 503 | $block_data['href'], |
| 504 | $block_data['button'], |
| 505 | 'addons-button-outline-purple' |
| 506 | ); |
| 507 | ?> |
| 508 | </div> |
| 509 | </div> |
| 510 | <?php |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Handles the outputting of featured sections |
| 515 | * |
| 516 | * @param array $sections Section data. |
| 517 | */ |
| 518 | public static function output_featured_sections( $sections ) { |
| 519 | foreach ( $sections as $section ) { |
| 520 | switch ( $section->module ) { |
| 521 | case 'banner_block': |
| 522 | self::output_banner_block( $section ); |
| 523 | break; |
| 524 | case 'column_start': |
| 525 | self::output_column( $section ); |
| 526 | break; |
| 527 | case 'column_end': |
| 528 | self::output_column( $section ); |
| 529 | break; |
| 530 | case 'column_block': |
| 531 | self::output_column_block( $section ); |
| 532 | break; |
| 533 | case 'small_light_block': |
| 534 | self::output_small_light_block( $section ); |
| 535 | break; |
| 536 | case 'small_dark_block': |
| 537 | self::output_small_dark_block( $section ); |
| 538 | break; |
| 539 | case 'wcs_banner_block': |
| 540 | self::output_wcs_banner_block( (array) $section ); |
| 541 | break; |
| 542 | case 'wcpay_banner_block': |
| 543 | self::output_wcpay_banner_block( (array) $section ); |
| 544 | break; |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Returns in-app-purchase URL params. |
| 551 | */ |
| 552 | public static function get_in_app_purchase_url_params() { |
| 553 | // Get url (from path onward) for the current page, |
| 554 | // so WCCOM "back" link returns user to where they were. |
| 555 | $back_admin_path = add_query_arg( array() ); |
| 556 | return array( |
| 557 | 'wccom-site' => site_url(), |
| 558 | 'wccom-back' => rawurlencode( $back_admin_path ), |
| 559 | 'wccom-woo-version' => Constants::get_constant( 'WC_VERSION' ), |
| 560 | 'wccom-connect-nonce' => wp_create_nonce( 'connect' ), |
| 561 | ); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Add in-app-purchase URL params to link. |
| 566 | * |
| 567 | * Adds various url parameters to a url to support a streamlined |
| 568 | * flow for obtaining and setting up WooCommerce extensons. |
| 569 | * |
| 570 | * @param string $url Destination URL. |
| 571 | */ |
| 572 | public static function add_in_app_purchase_url_params( $url ) { |
| 573 | return add_query_arg( |
| 574 | self::get_in_app_purchase_url_params(), |
| 575 | $url |
| 576 | ); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Outputs a button. |
| 581 | * |
| 582 | * @param string $url Destination URL. |
| 583 | * @param string $text Button label text. |
| 584 | * @param string $style Button style class. |
| 585 | * @param string $plugin The plugin the button is promoting. |
| 586 | */ |
| 587 | public static function output_button( $url, $text, $style, $plugin = '' ) { |
| 588 | $style = __( 'Free', 'woocommerce' ) === $text ? 'addons-button-outline-purple' : $style; |
| 589 | $style = is_plugin_active( $plugin ) ? 'addons-button-installed' : $style; |
| 590 | $text = is_plugin_active( $plugin ) ? __( 'Installed', 'woocommerce' ) : $text; |
| 591 | $url = self::add_in_app_purchase_url_params( $url ); |
| 592 | ?> |
| 593 | <a |
| 594 | class="addons-button <?php echo esc_attr( $style ); ?>" |
| 595 | href="<?php echo esc_url( $url ); ?>"> |
| 596 | <?php echo esc_html( $text ); ?> |
| 597 | </a> |
| 598 | <?php |
| 599 | } |
| 600 | |
| 601 | |
| 602 | /** |
| 603 | * Handles output of the addons page in admin. |
| 604 | */ |
| 605 | public static function output() { |
| 606 | $section = isset( $_GET['section'] ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : '_featured'; |
| 607 | $search = isset( $_GET['search'] ) ? sanitize_text_field( wp_unslash( $_GET['search'] ) ) : ''; |
| 608 | |
| 609 | if ( isset( $_GET['section'] ) && 'helper' === $_GET['section'] ) { |
| 610 | do_action( 'woocommerce_helper_output' ); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | if ( isset( $_GET['install-addon'] ) ) { |
| 615 | switch ( $_GET['install-addon'] ) { |
| 616 | case 'woocommerce-services': |
| 617 | self::install_woocommerce_services_addon(); |
| 618 | break; |
| 619 | case 'woocommerce-payments': |
| 620 | self::install_woocommerce_payments_addon(); |
| 621 | break; |
| 622 | default: |
| 623 | // Do nothing. |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | $sections = self::get_sections(); |
| 629 | $theme = wp_get_theme(); |
| 630 | $current_section = isset( $_GET['section'] ) ? $section : '_featured'; |
| 631 | $addons = array(); |
| 632 | |
| 633 | if ( '_featured' !== $current_section ) { |
| 634 | $category = $section ? $section : null; |
| 635 | $term = $search ? $search : null; |
| 636 | $country = WC()->countries->get_base_country(); |
| 637 | $addons = self::get_extension_data( $category, $term, $country ); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Addon page view. |
| 642 | * |
| 643 | * @uses $addons |
| 644 | * @uses $sections |
| 645 | * @uses $theme |
| 646 | * @uses $current_section |
| 647 | */ |
| 648 | include_once dirname( __FILE__ ) . '/views/html-admin-page-addons.php'; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Install WooCommerce Services from Extensions screens. |
| 653 | */ |
| 654 | public static function install_woocommerce_services_addon() { |
| 655 | check_admin_referer( 'install-addon_woocommerce-services' ); |
| 656 | |
| 657 | $services_plugin_id = 'woocommerce-services'; |
| 658 | $services_plugin = array( |
| 659 | 'name' => __( 'WooCommerce Services', 'woocommerce' ), |
| 660 | 'repo-slug' => 'woocommerce-services', |
| 661 | ); |
| 662 | |
| 663 | WC_Install::background_installer( $services_plugin_id, $services_plugin ); |
| 664 | |
| 665 | wp_safe_redirect( remove_query_arg( array( 'install-addon', '_wpnonce' ) ) ); |
| 666 | exit; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Install WooCommerce Payments from the Extensions screens. |
| 671 | * |
| 672 | * @return void |
| 673 | */ |
| 674 | public static function install_woocommerce_payments_addon() { |
| 675 | check_admin_referer( 'install-addon_woocommerce-payments' ); |
| 676 | |
| 677 | $wcpay_plugin_id = 'woocommerce-payments'; |
| 678 | $wcpay_plugin = array( |
| 679 | 'name' => __( 'WooCommerce Payments', 'woocommerce' ), |
| 680 | 'repo-slug' => 'woocommerce-payments', |
| 681 | ); |
| 682 | |
| 683 | WC_Install::background_installer( $services_plugin_id, $wcpay_plugin ); |
| 684 | |
| 685 | wp_safe_redirect( remove_query_arg( array( 'install-addon', '_wpnonce' ) ) ); |
| 686 | exit; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Should an extension be shown on the featured page. |
| 691 | * |
| 692 | * @param object $item Item data. |
| 693 | * @return boolean |
| 694 | */ |
| 695 | public static function show_extension( $item ) { |
| 696 | $location = WC()->countries->get_base_country(); |
| 697 | if ( isset( $item->geowhitelist ) && ! in_array( $location, $item->geowhitelist, true ) ) { |
| 698 | return false; |
| 699 | } |
| 700 | |
| 701 | if ( isset( $item->geoblacklist ) && in_array( $location, $item->geoblacklist, true ) ) { |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | if ( is_plugin_active( $item->plugin ) ) { |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | return true; |
| 710 | } |
| 711 | } |
| 712 |