WC_GZD_Hook_Priorities

The WooCommerce Germanized WC GZD Hook Priorities class.

Defined (1)

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

/includes/class-wc-gzd-hook-priorities.php  
  1. class WC_GZD_Hook_Priorities { 
  2.  
  3. /** 
  4. * Single instance of WC_GZD_Hook_Priorities 
  5. * @var object 
  6. */ 
  7. protected static $_instance = null; 
  8.  
  9. public $priorities = array(); 
  10. public $default_priorities = array(); 
  11. public $hooks = array(); 
  12. public $queue = array(); 
  13.  
  14. public static function instance() { 
  15. if ( is_null( self::$_instance ) ) { 
  16. self::$_instance = new self(); 
  17. return self::$_instance; 
  18.  
  19. /** 
  20. * Cloning is forbidden. 
  21. * @since 1.0 
  22. */ 
  23. public function __clone() { 
  24. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized-pro' ), '1.0' ); 
  25.  
  26. /** 
  27. * Unserializing instances of this class is forbidden. 
  28. * @since 1.0 
  29. */ 
  30. public function __wakeup() { 
  31. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized-pro' ), '1.0' ); 
  32.  
  33. public function __construct() { 
  34.  
  35. $this->init(); 
  36.  
  37. add_action( 'after_setup_theme', array( $this, 'renew_cache' ), 1 ); 
  38. add_action( 'after_setup_theme', array( $this, 'change_priority_queue' ), 2 ); 
  39.  
  40. public function init() { 
  41. // Default priorities used within WooCommerce (not customized by themes) 
  42. $this->default_priorities = array( 
  43. 'woocommerce_single_product_summary' => array(  
  44. 'woocommerce_template_single_price' => 10,  
  45. ),  
  46. 'woocommerce_checkout_order_review' => array( 
  47. 'woocommerce_order_review' => 10,  
  48. 'woocommerce_checkout_payment' => 20,  
  49. ),  
  50. 'woocommerce_thankyou' => array(  
  51. 'woocommerce_order_details_table' => 10,  
  52. ),  
  53. ); 
  54.  
  55. $this->priorities = $this->default_priorities; 
  56.  
  57. // Load custom theme priorities 
  58. if ( get_option( 'woocommerce_gzd_hook_priorities' ) ) 
  59. $this->priorities = (array) get_option( 'woocommerce_gzd_hook_priorities' ); 
  60.  
  61. $this->hooks = array( 
  62. 'single_price_unit' => $this->get_priority( 'woocommerce_single_product_summary', 'woocommerce_template_single_price' ) + 1,  
  63. 'single_legal_info' => $this->get_priority( 'woocommerce_single_product_summary', 'woocommerce_template_single_price' ) + 2,  
  64. 'single_delivery_time_info' => 27,  
  65. 'single_small_business_info' => 30,  
  66. 'single_product_units' => 5,  
  67. 'loop_price_unit' => 10,  
  68. 'loop_tax_info' => 6,  
  69. 'loop_shipping_costs_info' => 7,  
  70. 'loop_delivery_time_info' => 8,  
  71. 'loop_product_units' => 9,  
  72. 'cart_subtotal_unit_price' => 0,  
  73. 'cart_product_unit_price' => 0,  
  74. 'cart_product_units' => 10,  
  75. 'cart_product_delivery_time' => 11,  
  76. 'cart_product_item_desc' => 12,  
  77. 'cart_small_business_info' => 0,  
  78. 'checkout_small_business_info' => 25,  
  79. 'checkout_product_units' => 0,  
  80. 'checkout_product_delivery_time' => 0,  
  81. 'checkout_product_item_desc' => 0,  
  82. 'checkout_edit_data_notice' => 0,  
  83. 'checkout_payment' => 10,  
  84. 'checkout_order_review' => 20,  
  85. 'checkout_order_submit' => 21,  
  86. 'checkout_legal' => 2,  
  87. 'checkout_set_terms' => 3,  
  88. 'checkout_digital_checkbox' => 4,  
  89. 'checkout_service_checkbox' => 5,  
  90. 'checkout_direct_debit' => 6,  
  91. 'order_product_units' => 1,  
  92. 'order_product_delivery_time' => 2,  
  93. 'order_product_item_desc' => 3,  
  94. 'order_product_unit_price' => 0,  
  95. 'order_pay_now_button' => 0,  
  96. 'email_product_unit_price' => 0,  
  97. 'email_product_units' => 1,  
  98. 'email_product_delivery_time' => 2,  
  99. 'email_product_item_desc' => 3,  
  100. 'gzd_footer_vat_info' => 0,  
  101. 'footer_vat_info' => 5,  
  102. 'gzd_footer_sale_info' => 0,  
  103. 'footer_sale_info' => 5,  
  104. ); 
  105.  
  106.  
  107. /** 
  108. * Returns the priority for critical hooks (see $this->priorities) which may be customized by a theme 
  109. */ 
  110. public function get_priority( $hook, $function ) { 
  111. if ( isset( $this->priorities[ $hook ][ $function ] ) ) 
  112. return $this->priorities[ $hook ][ $function ]; 
  113. return false; 
  114.  
  115. /** 
  116. * Returns the priority for a custom wc germanized frontend hook 
  117. */ 
  118. public function get_hook_priority( $hook, $suppress_filters = false ) { 
  119. if ( isset( $this->hooks[ $hook ] ) ) 
  120. return ( ! $suppress_filters ? apply_filters( 'wc_gzd_frontend_hook_priority', $this->hooks[ $hook ], $hook, $this ) : $this->hooks[ $hook ] ); 
  121. return false; 
  122.  
  123. public function get_hook_priorities() { 
  124. return $this->hooks; 
  125.  
  126. /** 
  127. * This changes the hook priority by overriding customizations made by the theme 
  128. */ 
  129. public function change_priority( $hook, $function, $new_prio ) { 
  130. if ( ! $this->get_priority( $hook, $function ) ) 
  131. return false; 
  132. $this->queue[] = array( 'hook' => $hook, 'function' => $function, 'new_prio' => $new_prio ); 
  133.  
  134. /** 
  135. * Hooked by after_setup_theme. Not to be called directly 
  136. */ 
  137. public function change_priority_queue() { 
  138. if ( empty( $this->queue ) ) 
  139. return false; 
  140. foreach ( $this->queue as $queue ) { 
  141. remove_action( $queue[ 'hook' ], $queue[ 'function' ], $this->get_priority( $queue[ 'hook' ], $queue[ 'function' ] ) ); 
  142. add_action( $queue[ 'hook' ], $queue[ 'function' ], $queue[ 'new_prio' ] ); 
  143.  
  144. /** 
  145. * Regenerates the hook priority cache (checks for theme customizations) 
  146. */ 
  147. public function renew_cache() { 
  148. $this->priorities = $this->default_priorities; 
  149.  
  150. if ( ! empty( $this->priorities ) ) { 
  151.  
  152. foreach ( $this->priorities as $hook => $functions ) { 
  153.  
  154. foreach ( $functions as $function => $old_prio ) { 
  155.  
  156. $prio = has_action( $hook, $function ); 
  157.  
  158. if ( ! $prio ) 
  159. $prio = has_filter( $hook, $function ); 
  160.  
  161. if ( $prio ) 
  162. $this->priorities[ $hook ][ $function ] = $prio; 
  163. }  
  164. }  
  165.  
  166. if ( ! empty( $this->priorities ) ) 
  167. update_option( 'woocommerce_gzd_hook_priorities', $this->priorities ); 
  168. else 
  169. delete_option( 'woocommerce_gzd_hook_priorities' ); 
  170.  
  171.