WC_Admin_Addons

WC_Admin_Addons Class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-addons.php  
  1. class WC_Admin_Addons { 
  2.  
  3. /** 
  4. * Get featured for the addons screen 
  5. * @return array of objects 
  6. */ 
  7. public static function get_featured() { 
  8. if ( false === ( $featured = get_transient( 'wc_addons_featured' ) ) ) { 
  9. $raw_featured = wp_safe_remote_get( 'https://d3t0oesq8995hv.cloudfront.net/add-ons/featured.json', array( 'user-agent' => 'WooCommerce Addons Page' ) ); 
  10. if ( ! is_wp_error( $raw_featured ) ) { 
  11. $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); 
  12. if ( $featured ) { 
  13. set_transient( 'wc_addons_featured', $featured, WEEK_IN_SECONDS ); 
  14.  
  15. if ( is_object( $featured ) ) { 
  16. self::output_featured_sections( $featured->sections ); 
  17. return $featured; 
  18.  
  19. /** 
  20. * Get sections for the addons screen 
  21. * @return array of objects 
  22. */ 
  23. public static function get_sections() { 
  24. if ( false === ( $sections = get_transient( 'wc_addons_sections' ) ) ) { 
  25. $raw_sections = wp_safe_remote_get( 'https://d3t0oesq8995hv.cloudfront.net/addon-sections.json', array( 'user-agent' => 'WooCommerce Addons Page' ) ); 
  26. if ( ! is_wp_error( $raw_sections ) ) { 
  27. $sections = json_decode( wp_remote_retrieve_body( $raw_sections ) ); 
  28.  
  29. if ( $sections ) { 
  30. set_transient( 'wc_addons_sections', $sections, WEEK_IN_SECONDS ); 
  31.  
  32. $addon_sections = array(); 
  33.  
  34. if ( $sections ) { 
  35. foreach ( $sections as $sections_id => $section ) { 
  36. if ( empty( $sections_id ) ) { 
  37. continue; 
  38. $addon_sections[ $sections_id ] = new stdClass; 
  39. $addon_sections[ $sections_id ]->title = wc_clean( $section->title ); 
  40. $addon_sections[ $sections_id ]->endpoint = wc_clean( $section->endpoint ); 
  41.  
  42. return apply_filters( 'woocommerce_addons_sections', $addon_sections ); 
  43.  
  44. /** 
  45. * Get section for the addons screen. 
  46. * @param string $section_id 
  47. * @return object|bool 
  48. */ 
  49. public static function get_section( $section_id ) { 
  50. $sections = self::get_sections(); 
  51. if ( isset( $sections[ $section_id ] ) ) { 
  52. return $sections[ $section_id ]; 
  53. return false; 
  54.  
  55. /** 
  56. * Get section content for the addons screen. 
  57. * @param string $section_id 
  58. * @return array 
  59. */ 
  60. public static function get_section_data( $section_id ) { 
  61. $section = self::get_section( $section_id ); 
  62. $section_data = ''; 
  63.  
  64. if ( ! empty( $section->endpoint ) ) { 
  65. if ( false === ( $section_data = get_transient( 'wc_addons_section_' . $section_id ) ) ) { 
  66. $raw_section = wp_safe_remote_get( esc_url_raw( $section->endpoint ), array( 'user-agent' => 'WooCommerce Addons Page' ) ); 
  67.  
  68. if ( ! is_wp_error( $raw_section ) ) { 
  69. $section_data = json_decode( wp_remote_retrieve_body( $raw_section ) ); 
  70.  
  71. if ( ! empty( $section_data->products ) ) { 
  72. set_transient( 'wc_addons_section_' . $section_id, $section_data, WEEK_IN_SECONDS ); 
  73.  
  74. return apply_filters( 'woocommerce_addons_section_data', $section_data->products, $section_id ); 
  75.  
  76. /** 
  77. * Handles the outputting of a contextually aware Storefront link (points to child themes if Storefront is already active). 
  78. */ 
  79. public static function output_storefront_button() { 
  80. $template = get_option( 'template' ); 
  81. $stylesheet = get_option( 'stylesheet' ); 
  82.  
  83. if ( 'storefront' === $template ) { 
  84. if ( 'storefront' === $stylesheet ) { 
  85. $url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/'; 
  86. $text = __( 'Need a fresh look? Try Storefront child themes', 'woocommerce' ); 
  87. $utm_content = 'nostorefrontchildtheme'; 
  88. } else { 
  89. $url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/'; 
  90. $text = __( 'View more Storefront child themes', 'woocommerce' ); 
  91. $utm_content = 'hasstorefrontchildtheme'; 
  92. } else { 
  93. $url = 'https://woocommerce.com/storefront/'; 
  94. $text = __( 'Need a theme? Try Storefront', 'woocommerce' ); 
  95. $utm_content = 'nostorefront'; 
  96.  
  97. $url = add_query_arg( array( 
  98. 'utm_source' => 'addons',  
  99. 'utm_medium' => 'product',  
  100. 'utm_campaign' => 'woocommerceplugin',  
  101. 'utm_content' => $utm_content,  
  102. ), $url ); 
  103.  
  104. echo '<a href="' . esc_url( $url ) . '" class="add-new-h2">' . esc_html( $text ) . '</a>' . "\n"; 
  105.  
  106. /** 
  107. * Handles the outputting of a banner block. 
  108. * @param object $block 
  109. */ 
  110. public static function output_banner_block( $block ) { 
  111. ?> 
  112. <div class="addons-banner-block"> 
  113. <h1><?php echo esc_html( $block->title ); ?></h1> 
  114. <p><?php echo esc_html( $block->description ); ?></p> 
  115. <div class="addons-banner-block-items"> 
  116. <?php foreach ( $block->items as $item ) : ?> 
  117. <div class="addons-banner-block-item"> 
  118. <div class="addons-banner-block-item-icon"> 
  119. <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" /> 
  120. </div> 
  121. <div class="addons-banner-block-item-content"> 
  122. <h3><?php echo esc_html( $item->title ); ?></h3> 
  123. <p><?php echo esc_html( $item->description ); ?></p> 
  124. <?php 
  125. self::output_button( 
  126. $item->href,  
  127. $item->button,  
  128. 'addons-button-solid',  
  129. $item->plugin 
  130. ); 
  131. ?> 
  132. </div> 
  133. </div> 
  134. <?php endforeach; ?> 
  135. </div> 
  136. </div> 
  137. <?php 
  138.  
  139. /** 
  140. * Handles the outputting of a column. 
  141. * @param object $block 
  142. */ 
  143. public static function output_column( $block ) { 
  144. if ( isset( $block->container ) && 'column_container_start' === $block->container ) { 
  145. ?> 
  146. <div class="addons-column-section"> 
  147. <?php 
  148. if ( 'column_start' === $block->module ) { 
  149. ?> 
  150. <div class="addons-column"> 
  151. <?php 
  152. } else { 
  153. ?> 
  154. </div> 
  155. <?php 
  156. if ( isset( $block->container ) && 'column_container_end' === $block->container ) { 
  157. ?> 
  158. </div> 
  159. <?php 
  160.  
  161. /** 
  162. * Handles the outputting of a column block. 
  163. * @param object $block 
  164. */ 
  165. public static function output_column_block( $block ) { 
  166. ?> 
  167. <div class="addons-column-block"> 
  168. <h1><?php echo esc_html( $block->title ); ?></h1> 
  169. <p><?php echo esc_html( $block->description ); ?></p> 
  170. <?php foreach ( $block->items as $item ) : ?> 
  171. <div class="addons-column-block-item"> 
  172. <div class="addons-column-block-item-icon"> 
  173. <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" /> 
  174. </div> 
  175.  
  176. <div class="addons-column-block-item-content"> 
  177. <h2><?php echo esc_html( $item->title ); ?></h2> 
  178. <?php 
  179. self::output_button( 
  180. $item->href,  
  181. $item->button,  
  182. 'addons-button-solid',  
  183. $item->plugin 
  184. ); 
  185. ?> 
  186. <p><?php echo esc_html( $item->description ); ?></p> 
  187.  
  188. </div> 
  189. </div> 
  190. <?php endforeach; ?> 
  191. </div> 
  192.  
  193. <?php 
  194.  
  195. /** 
  196. * Handles the outputting of a small light block. 
  197. * @param object $block 
  198. */ 
  199. public static function output_small_light_block( $block ) { 
  200. ?> 
  201. <div class="addons-small-light-block"> 
  202. <img class="addons-img" src="<?php echo esc_url( $block->image ) ?>" /> 
  203. <div class="addons-small-light-block-content"> 
  204. <h1><?php echo esc_html( $block->title ); ?></h1> 
  205. <p><?php echo esc_html( $block->description ); ?></p> 
  206. <div class="addons-small-light-block-buttons"> 
  207. <?php foreach ( $block->buttons as $button ) : ?> 
  208. <?php 
  209. self::output_button( 
  210. $button->href,  
  211. $button->text,  
  212. 'addons-button-solid' 
  213. ); 
  214. ?> 
  215. <?php endforeach; ?> 
  216. </div> 
  217. </div> 
  218. </div> 
  219. <?php 
  220.  
  221. /** 
  222. * Handles the outputting of a small dark block. 
  223. * @param object $block 
  224. */ 
  225. public static function output_small_dark_block( $block ) { 
  226. ?> 
  227. <div class="addons-small-dark-block"> 
  228. <h1><?php echo esc_html( $block->title ); ?></h1> 
  229. <p><?php echo esc_html( $block->description ); ?></p> 
  230. <div class="addons-small-dark-items"> 
  231. <?php foreach ( $block->items as $item ) : ?> 
  232. <div class="addons-small-dark-item"> 
  233. <?php if ( ! empty( $item->image ) ) : ?> 
  234. <div class="addons-small-dark-item-icon"> 
  235. <img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" /> 
  236. </div> 
  237. <?php endif; ?> 
  238. <?php 
  239. self::output_button( 
  240. $item->href,  
  241. $item->button,  
  242. 'addons-button-outline-white' 
  243. ); 
  244. ?> 
  245. </div> 
  246. <?php endforeach; ?> 
  247. </div> 
  248. </div> 
  249. <?php 
  250.  
  251. /** 
  252. * Handles the outputting of featured sections 
  253. * @param array $sections 
  254. */ 
  255. public static function output_featured_sections( $sections ) { 
  256. foreach ( $sections as $section ) { 
  257. switch ( $section->module ) { 
  258. case 'banner_block': 
  259. self::output_banner_block( $section ); 
  260. break; 
  261. case 'column_start': 
  262. self::output_column( $section ); 
  263. break; 
  264. case 'column_end': 
  265. self::output_column( $section ); 
  266. break; 
  267. case 'column_block': 
  268. self::output_column_block( $section ); 
  269. break; 
  270. case 'small_light_block': 
  271. self::output_small_light_block( $section ); 
  272. break; 
  273. case 'small_dark_block': 
  274. self::output_small_dark_block( $section ); 
  275. break; 
  276.  
  277. /** 
  278. * Outputs a button. 
  279. * @param string $url 
  280. * @param string $text 
  281. * @param string $theme 
  282. * @param string $plugin 
  283. */ 
  284. public static function output_button( $url, $text, $theme, $plugin = '' ) { 
  285. $theme = __( 'Free', 'woocommerce' ) === $text ? 'addons-button-outline-green' : $theme; 
  286. $theme = is_plugin_active( $plugin ) ? 'addons-button-installed' : $theme; 
  287. $text = is_plugin_active( $plugin ) ? __( 'Installed', 'woocommerce' ) : $text; 
  288. ?> 
  289. <a 
  290. class="addons-button <?php echo esc_attr( $theme ); ?>" 
  291. href="<?php echo esc_url( $url ); ?>"> 
  292. <?php echo esc_html( $text ); ?> 
  293. </a> 
  294. <?php 
  295.  
  296.  
  297. /** 
  298. * Handles output of the addons page in admin. 
  299. */ 
  300. public static function output() { 
  301. $sections = self::get_sections(); 
  302. $theme = wp_get_theme(); 
  303. $section_keys = array_keys( $sections ); 
  304. $current_section = isset( $_GET['section'] ) ? sanitize_text_field( $_GET['section'] ) : current( $section_keys ); 
  305. include_once( dirname( __FILE__ ) . '/views/html-admin-page-addons.php' );