WCPBC_Admin_Regions_Table_List

The WooCommerce Product Price Based on Countries WCPBC Admin Regions Table List class.

Defined (1)

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

/includes/admin/class-wcpbc-admin-regions-table-list.php  
  1. class WCPBC_Admin_Regions_Table_List extends WP_List_Table { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. private $base_currency = NULL; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. private $default_region_key = NULL; 
  12.  
  13. /** 
  14. * Initialize the regions table list 
  15. */ 
  16. public function __construct() { 
  17.  
  18. $this->base_currency = get_option( 'woocommerce_currency' ); 
  19.  
  20. $this->default_region_key = uniqid('wc_price_based_country_default_region_key_'); 
  21.  
  22. parent::__construct( array( 
  23. 'singular' => __( 'Region', 'wc-price-based-country' ),  
  24. 'plural' => __( 'Regions', 'wc-price-based-country' ),  
  25. 'ajax' => false 
  26. ) ); 
  27.  
  28. /** 
  29. * Get list columns 
  30. * @return array 
  31. */ 
  32. public function get_columns() { 
  33. return array( 
  34. 'cb' => '<input type="checkbox" />',  
  35. 'name' => __( 'Region name', 'wc-price-based-country' ),  
  36. 'countries' => __( 'Countries', 'wc-price-based-country' ),  
  37. 'currency' => __( 'Currency', 'wc-price-based-country' ) 
  38. ); 
  39.  
  40. /** 
  41. * Column cb 
  42. * @param array $region 
  43. * @return string 
  44. */ 
  45. public function column_cb( $region ) { 
  46. if ( $region['key'] !== $this->default_region_key ) { 
  47. return sprintf( '<input type="checkbox" name="region_key[]" value="%1$s" />', $region['key'] ); 
  48. } else{ 
  49. return ''; 
  50.  
  51. /** 
  52. * Return name column 
  53. * @param array $region 
  54. * @return string 
  55. */ 
  56. public function column_name( $region ) {  
  57.  
  58. if ( $region['key'] !== $this->default_region_key ) { 
  59.  
  60. $url = admin_url( 'admin.php?page=wc-settings&tab=price-based-country§ion=regions&edit_region=' . $region['key'] );  
  61.  
  62. $output = '<strong>'; 
  63. $output .= '<a href="' . esc_url( $url ) . '" class="row-title">'; 
  64. if ( empty( $region['name'] ) ) { 
  65. $output .= esc_html__( 'Region name', 'wc-price-based-country' ); 
  66. } else { 
  67. $output .= esc_html( $region['name'] ); 
  68. $output .= '</a>'; 
  69. $output .= '</strong>';  
  70.  
  71. // Get actions 
  72. $actions = array( 
  73. 'id' => sprintf( 'Slug: %s', $region['key'] ),  
  74. 'edit' => '<a href="' . esc_url( $url ) . '">' . __( 'View/Edit', 'woocommerce' ) . '</a>',  
  75. 'trash' => '<a class="submitdelete" title="' . esc_attr__( 'Remove region', 'wc-price-based-country' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'remove_region' => $region['key'] ), admin_url( 'admin.php?page=wc-settings&tab=price-based-country§ion=regions' ) ), 'wc-price-based-country-remove-region' ) ) . '">' . __( 'Remove region', 'wc-price-based-country' ) . '</a>' 
  76. ); 
  77.  
  78. $row_actions = array(); 
  79.  
  80. foreach ( $actions as $action => $link ) { 
  81. $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>'; 
  82.  
  83. $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>'; 
  84.  
  85. } else { 
  86. $output = '<strong>' . $region['name'] . '</strong>'; 
  87.  
  88. return $output; 
  89.  
  90. /** 
  91. * Return countries column 
  92. * @param array $row 
  93. * @return string 
  94. */ 
  95. public function column_countries( $region ) { 
  96.  
  97. $display = ''; 
  98.  
  99. if ( is_array( $region['countries'] ) ) { 
  100.  
  101. $countries = array(); 
  102.  
  103. foreach( $region['countries'] as $iso_code ) { 
  104. $countries[] = WC()->countries->countries[$iso_code];  
  105. $display = implode($countries, ', '); 
  106.  
  107. } else { 
  108.  
  109. $display = $region['countries']; 
  110. }  
  111.  
  112. return $display;  
  113.  
  114. /** 
  115. * Return currency column 
  116. * @param array $row 
  117. * @return string 
  118. */ 
  119. public function column_currency( $region ) { 
  120. $currencies = get_woocommerce_currencies();  
  121.  
  122. $output = $currencies[$region['currency']] . ' (' . get_woocommerce_currency_symbol($region['currency']) . ') <br />'; 
  123.  
  124. if ( $region['key'] == $this->default_region_key ) { 
  125. $output .= '<span class="description">' . __( 'Default', 'wc-price-based-country' ) . '</span>'; 
  126. } else { 
  127. $output .= '<span class="description">1 ' . $this->base_currency .' = ' . wc_format_localized_decimal( $region['exchange_rate'] ) . ' ' . $region['currency'] . '</span>'; 
  128. return $output; 
  129.  
  130. /** 
  131. * Get bulk actions 
  132. * @return array 
  133. */ 
  134. protected function get_bulk_actions() { 
  135. return array( 
  136. 'remove' => __( 'Remove region', 'wc-price-based-country' ) 
  137. ); 
  138.  
  139. /** 
  140. * Prepare table list items. 
  141. */ 
  142. public function prepare_items() { 
  143.  
  144. $data = array( 
  145. array( 
  146. 'key' => $this->default_region_key,  
  147. 'name' => 'Default Zone',  
  148. 'countries' => 'All countries not are included in other zones',  
  149. 'currency' => $this->base_currency 
  150. );  
  151.  
  152. foreach ( get_option( 'wc_price_based_country_regions', array() ) as $key => $region) { 
  153. $data[] = array_merge( array('key'=> $key), $region ); 
  154.  
  155. $columns = $this->get_columns(); 
  156. $hidden = array(); 
  157. $sortable = array(); 
  158.  
  159.  
  160. $this->_column_headers = array($columns, $hidden, $sortable); 
  161.  
  162. $this->items = $data;