PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/class-stock-synchronization-admin.php

https://github.com/pronamic/wp-woocommerce-stock-synchronization
PHP | 228 lines | 161 code | 37 blank | 30 comment | 16 complexity | 14dc0f76dab9ed61b4f084fc559fe7b3 MD5 | raw file
  1. <?php
  2. /**
  3. * Provides the settings and admin page
  4. */
  5. class Stock_Synchronization_Admin {
  6. /**
  7. * Bootstraps the admin part
  8. */
  9. public static function bootstrap() {
  10. add_action( 'admin_init', array( __CLASS__, 'init' ) );
  11. add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
  12. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
  13. add_action( 'add_meta_boxes', array( __CLASS__, 'meta_boxes' ) );
  14. add_action( 'wp_ajax_stock_sync_single_product', array( __CLASS__, 'ajax_stock_synchronization' ) );
  15. }
  16. /**
  17. * Initializes admin
  18. */
  19. public static function init() {
  20. // Settings - Pages
  21. add_settings_section(
  22. 'woocommerce_stock_sync_general', // id
  23. __( 'General', 'woocommerce_stock_sync' ), // title
  24. '__return_false', // callback
  25. 'woocommerce_stock_sync' // page
  26. );
  27. add_settings_field(
  28. 'woocommerce_stock_sync_urls', // id
  29. __( 'URLs', 'pronamic_companies' ), // title
  30. array( __CLASS__, 'input_urls' ), // callback
  31. 'woocommerce_stock_sync', // page
  32. 'woocommerce_stock_sync_general', // section
  33. array( 'label_for' => 'woocommerce_stock_sync_urls' ) // args
  34. );
  35. add_settings_field(
  36. 'woocommerce_stock_sync_password', // id
  37. __( 'Password', 'woocommerce_stock_sync' ), // title
  38. array( __CLASS__, 'input_password' ), // callback
  39. 'woocommerce_stock_sync', // page
  40. 'woocommerce_stock_sync_general', // section
  41. array( 'label_for' => 'woocommerce_stock_sync_password' ) // args
  42. );
  43. register_setting( 'woocommerce_stock_sync', 'woocommerce_stock_sync_urls', array( __CLASS__, 'sanitize_urls' ) );
  44. register_setting( 'woocommerce_stock_sync', 'woocommerce_stock_sync_password' );
  45. }
  46. /**
  47. * Input text
  48. *
  49. * @param array $args
  50. */
  51. public static function input_password( $args ) {
  52. printf(
  53. '<input name="%s" id="%s" type="text" value="%s" class="%s" />',
  54. esc_attr( $args['label_for'] ),
  55. esc_attr( $args['label_for'] ),
  56. esc_attr( get_option( $args['label_for'] ) ),
  57. 'regular-text code'
  58. );
  59. }
  60. /**
  61. * Input text
  62. *
  63. * @param array $args
  64. */
  65. public static function input_urls( $args ) {
  66. $name = $args['label_for'];
  67. $urls = get_option( $name, array() );
  68. $i = '';
  69. foreach ( $urls as $url ) {
  70. printf(
  71. '<input name="%s[]" id="%s" type="url" value="%s" class="%s" />',
  72. esc_attr( $name ),
  73. esc_attr( $name . $i ),
  74. esc_attr( $url ),
  75. 'regular-text code'
  76. );
  77. echo '<br />';
  78. $i++;
  79. }
  80. printf(
  81. '<input name="%s[]" id="%s" type="url" value="%s" class="%s" />',
  82. esc_attr( $name ),
  83. esc_attr( $name . $i++ ),
  84. esc_attr( '' ),
  85. 'regular-text code'
  86. );
  87. }
  88. /**
  89. * Should be called on admin_menu hook. Adds settings pages to the admin menu.
  90. */
  91. public static function admin_menu() {
  92. add_submenu_page(
  93. 'woocommerce', // parent_slug
  94. __( 'WooCommerce Stock Synchronization', 'woocommerce_stock_sync' ), // page_title
  95. __( 'Stock Synchronization', 'woocommerce_stock_sync' ), // menu_title
  96. 'manage_options', // capability
  97. 'woocommerce_stock_sync', // menu_slug
  98. array( __CLASS__, 'settings_page' ) // function
  99. );
  100. }
  101. /**
  102. * Settings page
  103. */
  104. public static function settings_page() {
  105. include( Stock_Synchronization::get_plugin_path() . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'settings.php' );
  106. }
  107. /**
  108. * Sanitizes list of synched sites, unifying all newline characters to the same newline character
  109. */
  110. public static function sanitize_urls( $data ) {
  111. $urls = array();
  112. if ( is_array( $data ) ) {
  113. foreach ( $data as $value ) {
  114. $url = filter_var( $value, FILTER_VALIDATE_URL );
  115. if ( $url ) {
  116. $urls[] = trailingslashit( $url );
  117. }
  118. }
  119. }
  120. return $urls;
  121. }
  122. public static function enqueue_scripts() {
  123. wp_enqueue_script( 'woocommerce_stock_sync_admin', plugins_url( 'assets/stock-synchronization-admin.js', Stock_Synchronization::$file ) );
  124. wp_localize_script( 'woocommerce_stock_sync_admin', 'StockSynchronizationVars', array(
  125. 'single_product' => array(
  126. 'spinner' => admin_url( 'images/wpspin_light.gif' ),
  127. 'sync_success_success_message' => __( 'Synchronization was successful!', 'woocommerce_stock_sync' )
  128. )
  129. ) );
  130. }
  131. public static function meta_boxes() {
  132. add_meta_box(
  133. 'stock_synchronization',
  134. __( 'Stock Synchronization', 'woocommerce_stock_sync' ),
  135. array( __CLASS__, 'view_stock_synchronization_meta_box' ),
  136. 'product',
  137. 'side'
  138. );
  139. }
  140. public static function view_stock_synchronization_meta_box() {
  141. ?>
  142. <script type="text/javascript">
  143. jQuery(StockSynchronizationAdmin.single_product.ready);
  144. </script>
  145. <div class="stock_synchronization_holder">
  146. <div class="jStockSync"></div>
  147. <button class="jSyncSingleProductButton button"><?php _e( 'Push Stock', 'woocommerce_stock_sync' ); ?></button>
  148. <span class="jSync_spinner_holder"></span>
  149. </div>
  150. <?php
  151. }
  152. public static function ajax_stock_synchronization() {
  153. $post_type = filter_input( INPUT_POST, 'post_type', FILTER_SANITIZE_STRING );
  154. $post_id = filter_input( INPUT_POST, 'post_id', FILTER_VALIDATE_INT );
  155. if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '<' ) ) {
  156. if ( $post_type == 'product' ) {
  157. $product = new WC_Product( $post_id );
  158. } else if ( $post_type == 'product_variation' ) {
  159. $product = new WC_Product_Variation( $post_id );
  160. }
  161. } else {
  162. $product = get_product( $post_id );
  163. }
  164. // Get all variations
  165. $variations = get_posts( 'post_parent=' . $post_id . '&post_type=product_variation&orderby=menu_order&order=ASC&fields=ids&post_status=any&numberposts=-1' );
  166. foreach ( Stock_Synchronization::$synced_sites as $site ) {
  167. $result = Stock_Synchronization_Synchronizer::synchronize_product( $product, $site );
  168. if ( $result instanceof WP_Error ) {
  169. $response = array( 'url' => $site, 'resp' => false, 'errors' => $result->get_error_messages() );
  170. } else {
  171. $response = array( 'url' => $site, 'resp' => true );
  172. }
  173. if ( ! empty( $variations ) ) {
  174. foreach ( $variations as $variation ) {
  175. if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '<' ) ) {
  176. $variation_product = new WC_Product_Variation( $variation );
  177. } else {
  178. $variation_product = get_product( $variation );
  179. }
  180. $result = Stock_Synchronization_Synchronizer::synchronize_product( $variation_product, $site );
  181. if ( $result instanceof WP_Error ) {
  182. $response['variations'][] = array( 'url' => $site, 'resp' => false, 'errors' => $result->get_error_messages() );
  183. } else {
  184. $response['variations'][] = array( 'url' => $site, 'resp' => true );
  185. }
  186. }
  187. }
  188. }
  189. wp_send_json( $response );
  190. }
  191. }