class-wc-admin-addons.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. /**
  3. * Addons Page
  4. *
  5. * @author WooThemes
  6. * @category Admin
  7. * @package WooCommerce/Admin
  8. * @version 2.5.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * WC_Admin_Addons Class.
  15. */
  16. class WC_Admin_Addons {
  17. /**
  18. * Get featured for the addons screen
  19. *
  20. * @return array of objects
  21. */
  22. public static function get_featured() {
  23. if ( false === ( $featured = get_transient( 'wc_addons_featured' ) ) ) {
  24. $raw_featured = wp_safe_remote_get( 'https://d3t0oesq8995hv.cloudfront.net/add-ons/featured-v2.json', array( 'user-agent' => 'WooCommerce Addons Page' ) );
  25. if ( ! is_wp_error( $raw_featured ) ) {
  26. $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) );
  27. if ( $featured ) {
  28. set_transient( 'wc_addons_featured', $featured, WEEK_IN_SECONDS );
  29. }
  30. }
  31. }
  32. if ( is_object( $featured ) ) {
  33. self::output_featured_sections( $featured->sections );
  34. return $featured;
  35. }
  36. }
  37. /**
  38. * Build url parameter string
  39. *
  40. * @param string $category
  41. * @param string $term
  42. * @param string $country
  43. *
  44. * @return string url parameter string
  45. */
  46. public static function build_parameter_string( $category, $term, $country ) {
  47. $paramters = array(
  48. 'category' => $category,
  49. 'term' => $term,
  50. 'country' => $country,
  51. );
  52. return '?' . http_build_query( $paramters );
  53. }
  54. /**
  55. * Call API to get extensions
  56. *
  57. * @param string $category
  58. * @param string $term
  59. * @param string $country
  60. *
  61. * @return array of extensions
  62. */
  63. public static function get_extension_data( $category, $term, $country ) {
  64. $parameters = self::build_parameter_string( $category, $term, $country );
  65. $raw_extensions = wp_remote_get(
  66. 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters
  67. );
  68. if ( ! is_wp_error( $raw_extensions ) ) {
  69. $addons = json_decode( wp_remote_retrieve_body( $raw_extensions ) )->products;
  70. }
  71. return $addons;
  72. }
  73. /**
  74. * Get sections for the addons screen
  75. *
  76. * @return array of objects
  77. */
  78. public static function get_sections() {
  79. $addon_sections = get_transient( 'wc_addons_sections' );
  80. if ( false === ( $addon_sections ) ) {
  81. $raw_sections = wp_safe_remote_get(
  82. 'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories'
  83. );
  84. if ( ! is_wp_error( $raw_sections ) ) {
  85. $addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) );
  86. if ( $addon_sections ) {
  87. set_transient( 'wc_addons_sections', $addon_sections, WEEK_IN_SECONDS );
  88. }
  89. }
  90. }
  91. return apply_filters( 'woocommerce_addons_sections', $addon_sections );
  92. }
  93. /**
  94. * Get section for the addons screen.
  95. *
  96. * @param string $section_id
  97. *
  98. * @return object|bool
  99. */
  100. public static function get_section( $section_id ) {
  101. $sections = self::get_sections();
  102. if ( isset( $sections[ $section_id ] ) ) {
  103. return $sections[ $section_id ];
  104. }
  105. return false;
  106. }
  107. /**
  108. * Get section content for the addons screen.
  109. *
  110. * @param string $section_id
  111. *
  112. * @return array
  113. */
  114. public static function get_section_data( $section_id ) {
  115. $section = self::get_section( $section_id );
  116. $section_data = '';
  117. if ( ! empty( $section->endpoint ) ) {
  118. if ( false === ( $section_data = get_transient( 'wc_addons_section_' . $section_id ) ) ) {
  119. $raw_section = wp_safe_remote_get( esc_url_raw( $section->endpoint ), array( 'user-agent' => 'WooCommerce Addons Page' ) );
  120. if ( ! is_wp_error( $raw_section ) ) {
  121. $section_data = json_decode( wp_remote_retrieve_body( $raw_section ) );
  122. if ( ! empty( $section_data->products ) ) {
  123. set_transient( 'wc_addons_section_' . $section_id, $section_data, WEEK_IN_SECONDS );
  124. }
  125. }
  126. }
  127. }
  128. return apply_filters( 'woocommerce_addons_section_data', $section_data->products, $section_id );
  129. }
  130. /**
  131. * Handles the outputting of a contextually aware Storefront link (points to child themes if Storefront is already active).
  132. */
  133. public static function output_storefront_button() {
  134. $template = get_option( 'template' );
  135. $stylesheet = get_option( 'stylesheet' );
  136. if ( 'storefront' === $template ) {
  137. if ( 'storefront' === $stylesheet ) {
  138. $url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/';
  139. $text = __( 'Need a fresh look? Try Storefront child themes', 'woocommerce' );
  140. $utm_content = 'nostorefrontchildtheme';
  141. } else {
  142. $url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/';
  143. $text = __( 'View more Storefront child themes', 'woocommerce' );
  144. $utm_content = 'hasstorefrontchildtheme';
  145. }
  146. } else {
  147. $url = 'https://woocommerce.com/storefront/';
  148. $text = __( 'Need a theme? Try Storefront', 'woocommerce' );
  149. $utm_content = 'nostorefront';
  150. }
  151. $url = add_query_arg(
  152. array(
  153. 'utm_source' => 'addons',
  154. 'utm_medium' => 'product',
  155. 'utm_campaign' => 'woocommerceplugin',
  156. 'utm_content' => $utm_content,
  157. ), $url
  158. );
  159. echo '<a href="' . esc_url( $url ) . '" class="add-new-h2">' . esc_html( $text ) . '</a>' . "\n";
  160. }
  161. /**
  162. * Handles the outputting of a banner block.
  163. *
  164. * @param object $block
  165. */
  166. public static function output_banner_block( $block ) {
  167. ?>
  168. <div class="addons-banner-block">
  169. <h1><?php echo esc_html( $block->title ); ?></h1>
  170. <p><?php echo esc_html( $block->description ); ?></p>
  171. <div class="addons-banner-block-items">
  172. <?php foreach ( $block->items as $item ) : ?>
  173. <?php if ( self::show_extension( $item ) ) : ?>
  174. <div class="addons-banner-block-item">
  175. <div class="addons-banner-block-item-icon">
  176. <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" />
  177. </div>
  178. <div class="addons-banner-block-item-content">
  179. <h3><?php echo esc_html( $item->title ); ?></h3>
  180. <p><?php echo esc_html( $item->description ); ?></p>
  181. <?php
  182. self::output_button(
  183. $item->href,
  184. $item->button,
  185. 'addons-button-solid',
  186. $item->plugin
  187. );
  188. ?>
  189. </div>
  190. </div>
  191. <?php endif; ?>
  192. <?php endforeach; ?>
  193. </div>
  194. </div>
  195. <?php
  196. }
  197. /**
  198. * Handles the outputting of a column.
  199. *
  200. * @param object $block
  201. */
  202. public static function output_column( $block ) {
  203. if ( isset( $block->container ) && 'column_container_start' === $block->container ) {
  204. ?>
  205. <div class="addons-column-section">
  206. <?php
  207. }
  208. if ( 'column_start' === $block->module ) {
  209. ?>
  210. <div class="addons-column">
  211. <?php
  212. } else {
  213. ?>
  214. </div>
  215. <?php
  216. }
  217. if ( isset( $block->container ) && 'column_container_end' === $block->container ) {
  218. ?>
  219. </div>
  220. <?php
  221. }
  222. }
  223. /**
  224. * Handles the outputting of a column block.
  225. *
  226. * @param object $block
  227. */
  228. public static function output_column_block( $block ) {
  229. ?>
  230. <div class="addons-column-block">
  231. <h1><?php echo esc_html( $block->title ); ?></h1>
  232. <p><?php echo esc_html( $block->description ); ?></p>
  233. <?php foreach ( $block->items as $item ) : ?>
  234. <?php if ( self::show_extension( $item ) ) : ?>
  235. <div class="addons-column-block-item">
  236. <div class="addons-column-block-item-icon">
  237. <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" />
  238. </div>
  239. <div class="addons-column-block-item-content">
  240. <h2><?php echo esc_html( $item->title ); ?></h2>
  241. <?php
  242. self::output_button(
  243. $item->href,
  244. $item->button,
  245. 'addons-button-solid',
  246. $item->plugin
  247. );
  248. ?>
  249. <p><?php echo esc_html( $item->description ); ?></p>
  250. </div>
  251. </div>
  252. <?php endif; ?>
  253. <?php endforeach; ?>
  254. </div>
  255. <?php
  256. }
  257. /**
  258. * Handles the outputting of a small light block.
  259. *
  260. * @param object $block
  261. */
  262. public static function output_small_light_block( $block ) {
  263. ?>
  264. <div class="addons-small-light-block">
  265. <img class="addons-img" src="<?php echo esc_url( $block->image ); ?>" />
  266. <div class="addons-small-light-block-content">
  267. <h1><?php echo esc_html( $block->title ); ?></h1>
  268. <p><?php echo esc_html( $block->description ); ?></p>
  269. <div class="addons-small-light-block-buttons">
  270. <?php foreach ( $block->buttons as $button ) : ?>
  271. <?php
  272. self::output_button(
  273. $button->href,
  274. $button->text,
  275. 'addons-button-solid'
  276. );
  277. ?>
  278. <?php endforeach; ?>
  279. </div>
  280. </div>
  281. </div>
  282. <?php
  283. }
  284. /**
  285. * Handles the outputting of a small dark block.
  286. *
  287. * @param object $block
  288. */
  289. public static function output_small_dark_block( $block ) {
  290. ?>
  291. <div class="addons-small-dark-block">
  292. <h1><?php echo esc_html( $block->title ); ?></h1>
  293. <p><?php echo esc_html( $block->description ); ?></p>
  294. <div class="addons-small-dark-items">
  295. <?php foreach ( $block->items as $item ) : ?>
  296. <div class="addons-small-dark-item">
  297. <?php if ( ! empty( $item->image ) ) : ?>
  298. <div class="addons-small-dark-item-icon">
  299. <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" />
  300. </div>
  301. <?php endif; ?>
  302. <?php
  303. self::output_button(
  304. $item->href,
  305. $item->button,
  306. 'addons-button-outline-white'
  307. );
  308. ?>
  309. </div>
  310. <?php endforeach; ?>
  311. </div>
  312. </div>
  313. <?php
  314. }
  315. /**
  316. * Handles the outputting of the WooCommerce Services banner block.
  317. *
  318. * @param object $block
  319. */
  320. public static function output_wcs_banner_block( $block = array() ) {
  321. $is_active = is_plugin_active( 'woocommerce-services/woocommerce-services.php' );
  322. $location = wc_get_base_location();
  323. if (
  324. ! in_array( $location['country'], array( 'US', 'CA' ), true ) ||
  325. $is_active ||
  326. ! current_user_can( 'install_plugins' ) ||
  327. ! current_user_can( 'activate_plugins' )
  328. ) {
  329. return;
  330. }
  331. $button_url = wp_nonce_url(
  332. add_query_arg(
  333. array(
  334. 'install-addon' => 'woocommerce-services',
  335. )
  336. ),
  337. 'install-addon_woocommerce-services'
  338. );
  339. $defaults = array(
  340. 'image' => WC()->plugin_url() . '/assets/images/wcs-extensions-banner-3x.png',
  341. 'image_alt' => __( 'WooCommerce Services', 'woocommerce' ),
  342. 'title' => __( 'Buy discounted shipping labels — then print them from your dashboard.', 'woocommerce' ),
  343. 'description' => __( 'Integrate your store with USPS to buy discounted shipping labels, and print them directly from your WooCommerce dashboard. Powered by WooCommerce Services.', 'woocommerce' ),
  344. 'button' => __( 'Free - Install now', 'woocommerce' ),
  345. 'href' => $button_url,
  346. 'logos' => array(),
  347. );
  348. switch ( $location['country'] ) {
  349. case 'CA':
  350. $local_defaults = array(
  351. 'image' => WC()->plugin_url() . '/assets/images/wcs-truck-banner-3x.png',
  352. 'title' => __( 'Show Canada Post shipping rates', 'woocommerce' ),
  353. 'description' => __( 'Display live rates from Canada Post at checkout to make shipping a breeze. Powered by WooCommerce Services.', 'woocommerce' ),
  354. 'logos' => array_merge(
  355. $defaults['logos'], array(
  356. array(
  357. 'link' => WC()->plugin_url() . '/assets/images/wcs-canada-post-logo.jpg',
  358. 'alt' => 'Canada Post logo',
  359. ),
  360. )
  361. ),
  362. );
  363. break;
  364. case 'US':
  365. $local_defaults = array(
  366. 'logos' => array_merge(
  367. $defaults['logos'], array(
  368. array(
  369. 'link' => WC()->plugin_url() . '/assets/images/wcs-usps-logo.png',
  370. 'alt' => 'USPS logo',
  371. ),
  372. )
  373. ),
  374. );
  375. break;
  376. default:
  377. $local_defaults = array();
  378. }
  379. $block_data = array_merge( $defaults, $local_defaults, $block );
  380. ?>
  381. <div class="addons-wcs-banner-block">
  382. <div class="addons-wcs-banner-block-image">
  383. <img
  384. class="addons-img"
  385. src="<?php echo esc_url( $block_data['image'] ); ?>"
  386. alt="<?php echo esc_attr( $block_data['image_alt'] ); ?>"
  387. />
  388. </div>
  389. <div class="addons-wcs-banner-block-content">
  390. <h1><?php echo esc_html( $block_data['title'] ); ?></h1>
  391. <p><?php echo esc_html( $block_data['description'] ); ?></p>
  392. <ul>
  393. <?php foreach ( $block_data['logos'] as $logo ) : ?>
  394. <li>
  395. <img
  396. alt="<?php echo esc_url( $logo['alt'] ); ?>"
  397. class="wcs-service-logo"
  398. src="<?php echo esc_url( $logo['link'] ); ?>"
  399. >
  400. </li>
  401. <?php endforeach; ?>
  402. </ul>
  403. <?php
  404. self::output_button(
  405. $block_data['href'],
  406. $block_data['button'],
  407. 'addons-button-outline-green'
  408. );
  409. ?>
  410. </div>
  411. </div>
  412. <?php
  413. }
  414. /**
  415. * Handles the outputting of featured sections
  416. *
  417. * @param array $sections
  418. */
  419. public static function output_featured_sections( $sections ) {
  420. foreach ( $sections as $section ) {
  421. switch ( $section->module ) {
  422. case 'banner_block':
  423. self::output_banner_block( $section );
  424. break;
  425. case 'column_start':
  426. self::output_column( $section );
  427. break;
  428. case 'column_end':
  429. self::output_column( $section );
  430. break;
  431. case 'column_block':
  432. self::output_column_block( $section );
  433. break;
  434. case 'small_light_block':
  435. self::output_small_light_block( $section );
  436. break;
  437. case 'small_dark_block':
  438. self::output_small_dark_block( $section );
  439. break;
  440. case 'wcs_banner_block':
  441. self::output_wcs_banner_block( (array) $section );
  442. break;
  443. }
  444. }
  445. }
  446. /**
  447. * Outputs a button.
  448. *
  449. * @param string $url
  450. * @param string $text
  451. * @param string $theme
  452. * @param string $plugin
  453. */
  454. public static function output_button( $url, $text, $theme, $plugin = '' ) {
  455. $theme = __( 'Free', 'woocommerce' ) === $text ? 'addons-button-outline-green' : $theme;
  456. $theme = is_plugin_active( $plugin ) ? 'addons-button-installed' : $theme;
  457. $text = is_plugin_active( $plugin ) ? __( 'Installed', 'woocommerce' ) : $text;
  458. ?>
  459. <a
  460. class="addons-button <?php echo esc_attr( $theme ); ?>"
  461. href="<?php echo esc_url( $url ); ?>">
  462. <?php echo esc_html( $text ); ?>
  463. </a>
  464. <?php
  465. }
  466. /**
  467. * Handles output of the addons page in admin.
  468. */
  469. public static function output() {
  470. if ( isset( $_GET['section'] ) && 'helper' === $_GET['section'] ) {
  471. do_action( 'woocommerce_helper_output' );
  472. return;
  473. }
  474. if ( isset( $_GET['install-addon'] ) && 'woocommerce-services' === $_GET['install-addon'] ) {
  475. self::install_woocommerce_services_addon();
  476. }
  477. $sections = self::get_sections();
  478. $theme = wp_get_theme();
  479. $current_section = isset( $_GET['section'] ) ? sanitize_text_field( $_GET['section'] ) : '_featured';
  480. $addons = array();
  481. if ( '_featured' !== $current_section ) {
  482. $category = isset( $_GET['section'] ) ? $_GET['section'] : null;
  483. $term = isset( $_GET['search'] ) ? $_GET['search'] : null;
  484. $country = WC()->countries->get_base_country();
  485. $addons = self::get_extension_data( $category, $term, $country );
  486. }
  487. /**
  488. * Addon page view.
  489. *
  490. * @uses $addons
  491. * @uses $sections
  492. * @uses $theme
  493. * @uses $current_section
  494. */
  495. include_once dirname( __FILE__ ) . '/views/html-admin-page-addons.php';
  496. }
  497. /**
  498. * Install WooCommerce Services from Extensions screens.
  499. */
  500. public static function install_woocommerce_services_addon() {
  501. check_admin_referer( 'install-addon_woocommerce-services' );
  502. $services_plugin_id = 'woocommerce-services';
  503. $services_plugin = array(
  504. 'name' => __( 'WooCommerce Services', 'woocommerce' ),
  505. 'repo-slug' => 'woocommerce-services',
  506. );
  507. WC_Install::background_installer( $services_plugin_id, $services_plugin );
  508. wp_safe_redirect( remove_query_arg( array( 'install-addon', '_wpnonce' ) ) );
  509. exit;
  510. }
  511. /**
  512. * Should an extension be shown on the featured page.
  513. *
  514. * @param object $item
  515. * @return boolean
  516. */
  517. public static function show_extension( $item ) {
  518. $location = WC()->countries->get_base_country();
  519. if ( isset( $item->geowhitelist ) && ! in_array( $location, $item->geowhitelist, true ) ) {
  520. return false;
  521. }
  522. if ( isset( $item->geoblacklist ) && in_array( $location, $item->geoblacklist, true ) ) {
  523. return false;
  524. }
  525. if ( is_plugin_active( $item->plugin ) ) {
  526. return false;
  527. }
  528. return true;
  529. }
  530. }