From 06ef8c8589352ee4632dea4443547776dff6c663 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 2 May 2018 14:28:53 +0100 Subject: [PATCH] Hook doc update --- apigen/hook-docs.php | 107 +++++++++--------- includes/admin/class-wc-admin-menus.php | 4 +- includes/class-wc-ajax.php | 3 +- includes/class-wc-install.php | 3 +- .../class-wc-product-data-store-cpt.php | 12 +- .../class-wc-email-customer-invoice.php | 19 ++-- includes/wc-stock-functions.php | 6 +- templates/cart/cart-shipping.php | 8 +- 8 files changed, 92 insertions(+), 70 deletions(-) diff --git a/apigen/hook-docs.php b/apigen/hook-docs.php index 7de5461a75d..74087e50198 100644 --- a/apigen/hook-docs.php +++ b/apigen/hook-docs.php @@ -58,34 +58,42 @@ class WC_HookFinder { } public static function process_hooks() { - // If we have one, get the PHP files from it. - $template_files = self::get_files( '*.php', GLOB_MARK, '../templates/' ); - $template_files[] = '../includes/wc-template-functions.php'; - $template_files[] = '../includes/wc-template-hooks.php'; + self::$files_to_scan = array(); - $shortcode_files = self::get_files( '*.php', GLOB_MARK, '../includes/shortcodes/' ); - $widget_files = self::get_files( '*.php', GLOB_MARK, '../includes/widgets/' ); - $admin_files = self::get_files( '*.php', GLOB_MARK, '../includes/admin/' ); - $class_files = self::get_files( '*.php', GLOB_MARK, '../includes/' ); - $other_files = array( - '../woocommerce.php' + self::$files_to_scan['Template Files'] = self::get_files( '*.php', GLOB_MARK, '../templates/' ); + self::$files_to_scan['Template Functions'] = array( '../includes/wc-template-functions.php', '../includes/wc-template-hooks.php' ); + self::$files_to_scan['Shortcodes'] = self::get_files( '*.php', GLOB_MARK, '../includes/shortcodes/' ); + self::$files_to_scan['Widgets'] = self::get_files( '*.php', GLOB_MARK, '../includes/widgets/' ); + self::$files_to_scan['Data Stores'] = self::get_files( '*.php', GLOB_MARK, '../includes/data-stores' ); + self::$files_to_scan['Core Classes'] = array_merge( + self::get_files( '*.php', GLOB_MARK, '../includes/' ), + self::get_files( '*.php', GLOB_MARK, '../includes/abstracts/' ), + self::get_files( '*.php', GLOB_MARK, '../includes/customizer/' ), + self::get_files( '*.php', GLOB_MARK, '../includes/emails/' ), + self::get_files( '*.php', GLOB_MARK, '../includes/export/' ), + self::get_files( '*.php', GLOB_MARK, '../includes/gateways/' ), + self::get_files( '*.php', GLOB_MARK, '../includes/import/' ), + self::get_files( '*.php', GLOB_MARK, '../includes/shipping/' ) ); - self::$files_to_scan = array( - 'Template Hooks' => $template_files, - 'Shortcode Hooks' => $shortcode_files, - 'Widget Hooks' => $widget_files, - 'Class Hooks' => $class_files, - 'Admin Hooks' => $admin_files, - 'Other Hooks' => $other_files, - ); + self::$files_to_scan = array_filter( self::$files_to_scan ); $scanned = array(); ob_start(); + $index = array(); + + foreach ( self::$files_to_scan as $heading => $files ) { + $index[] = '' . $heading . ''; + } + echo '
'; echo '

Action and Filter Hook Reference

'; + echo '
+

This is simply a list of action and filter hooks found within WooCommerce files. View the source to see supported params and usage.

+

' . implode( ', ', $index ) . '

+
'; foreach ( self::$files_to_scan as $heading => $files ) { self::$custom_hooks_found = array(); @@ -125,43 +133,36 @@ class WC_HookFinder { case 'filter' : case 'action' : $hook = trim( $token[1], "'" ); + $hook = str_replace( '_FUNCTION_', strtoupper( $current_function ), $hook ); + $hook = str_replace( '_CLASS_', strtoupper( $current_class ), $hook ); + $hook = str_replace( '$this', strtoupper( $current_class ), $hook ); + $hook = str_replace( array( '.', '{', '}', '"', "'", ' ', ')', '(' ), '', $hook ); $loop = 0; - if ( '_' === substr( $hook, '-1', 1 ) ) { - $hook .= '{'; - $open = true; - // Keep adding to hook until we find a comma or colon - while ( 1 ) { - $loop ++; - $next_hook = trim( trim( is_string( $tokens[ $index + $loop ] ) ? $tokens[ $index + $loop ] : $tokens[ $index + $loop ][1], '"' ), "'" ); + // Keep adding to hook until we find a comma or colon + while ( 1 ) { + $loop ++; + $prev_hook = is_string( $tokens[ $index + $loop - 1 ] ) ? $tokens[ $index + $loop - 1 ] : $tokens[ $index + $loop - 1 ][1]; + $next_hook = is_string( $tokens[ $index + $loop ] ) ? $tokens[ $index + $loop ] : $tokens[ $index + $loop ][1]; - if ( in_array( $next_hook, array( '.', '{', '}', '"', "'", ' ' ) ) ) { - continue; - } - - $hook_first = substr( $next_hook, 0, 1 ); - $hook_last = substr( $next_hook, -1, 1 ); - - if ( in_array( $next_hook, array( ',', ';' ) ) ) { - if ( $open ) { - $hook .= '}'; - $open = false; - } - break; - } - - if ( '_' === $hook_first ) { - $next_hook = '}' . $next_hook; - $open = false; - } - - if ( '_' === $hook_last ) { - $next_hook .= '{'; - $open = true; - } - - $hook .= $next_hook; + if ( in_array( $next_hook, array( '.', '{', '}', '"', "'", ' ', ')', '(' ) ) ) { + continue; } + + if ( in_array( $next_hook, array( ',', ';' ) ) ) { + break; + } + + $hook_first = substr( $next_hook, 0, 1 ); + $hook_last = substr( $next_hook, -1, 1 ); + + if ( '{' === $hook_first || '}' === $hook_last || '$' === $hook_first || ')' === $hook_last || '>' === substr( $prev_hook, -1, 1 ) ) { + $next_hook = strtoupper( $next_hook ); + } + + $next_hook = str_replace( array( '.', '{', '}', '"', "'", ' ', ')', '(' ), '', $next_hook ); + + $hook .= $next_hook; } if ( isset( self::$custom_hooks_found[ $hook ] ) ) { @@ -185,14 +186,14 @@ class WC_HookFinder { foreach ( self::$custom_hooks_found as $hook => $details ) { if ( ! strstr( $hook, 'woocommerce' ) && ! strstr( $hook, 'product' ) && ! strstr( $hook, 'wc_' ) ) { - unset( self::$custom_hooks_found[ $hook ] ); + //unset( self::$custom_hooks_found[ $hook ] ); } } ksort( self::$custom_hooks_found ); if ( ! empty( self::$custom_hooks_found ) ) { - echo '

' . $heading . '

'; + echo '

' . $heading . '

'; echo ''; diff --git a/includes/admin/class-wc-admin-menus.php b/includes/admin/class-wc-admin-menus.php index ca700685fd9..62f8674ad0d 100644 --- a/includes/admin/class-wc-admin-menus.php +++ b/includes/admin/class-wc-admin-menus.php @@ -98,7 +98,9 @@ class WC_Admin_Menus { $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( wp_unslash( $_REQUEST['section'] ) ); // WPCS: input var okay, CSRF ok. // Save settings if data has been posted. - if ( apply_filters( '' !== $current_section ? "woocommerce_save_settings_{$current_tab}_{$current_section}" : "woocommerce_save_settings_{$current_tab}", ! empty( $_POST ) ) ) { // WPCS: input var okay, CSRF ok. + if ( '' !== $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}_{$current_section}", ! empty( $_POST ) ) ) { // WPCS: input var okay, CSRF ok. + WC_Admin_Settings::save(); + } elseif ( apply_filters( "woocommerce_save_settings_{$current_tab}", ! empty( $_POST ) ) ) { // WPCS: input var okay, CSRF ok. WC_Admin_Settings::save(); } diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 408a30860f8..515d10268bc 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -74,7 +74,8 @@ class WC_AJAX { if ( $action ) { self::wc_ajax_headers(); - do_action( 'wc_ajax_' . sanitize_text_field( $action ) ); + $action = sanitize_text_field( $action ); + do_action( 'wc_ajax_' . $action ); wp_die(); } } diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index ee80972196b..6ffb7f8e19d 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -158,7 +158,8 @@ class WC_Install { WC_Admin_Notices::add_notice( 'update' ); } if ( ! empty( $_GET['force_update_woocommerce'] ) ) { // WPCS: input var ok, CSRF ok. - do_action( 'wp_' . get_current_blog_id() . '_wc_updater_cron' ); + $blog_id = get_current_blog_id(); + do_action( 'wp_' . $blog_id . '_wc_updater_cron' ); wp_safe_redirect( admin_url( 'admin.php?page=wc-settings' ) ); exit; } diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index d96aa24f045..ef96e39718a 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -611,11 +611,19 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da } if ( in_array( 'stock_quantity', $this->updated_props, true ) ) { - do_action( $product->is_type( 'variation' ) ? 'woocommerce_variation_set_stock' : 'woocommerce_product_set_stock', $product ); + if ( $product->is_type( 'variation' ) ) { + do_action( 'woocommerce_variation_set_stock', $product ); + } else { + do_action( 'woocommerce_product_set_stock', $product ); + } } if ( in_array( 'stock_status', $this->updated_props, true ) ) { - do_action( $product->is_type( 'variation' ) ? 'woocommerce_variation_set_stock_status' : 'woocommerce_product_set_stock_status', $product->get_id(), $product->get_stock_status(), $product ); + if ( $product->is_type( 'variation' ) ) { + do_action( 'woocommerce_variation_set_stock_status', $product->get_id(), $product->get_stock_status(), $product ); + } else { + do_action( 'woocommerce_product_set_stock_status', $product->get_id(), $product->get_stock_status(), $product ); + } } // Trigger action so 3rd parties can deal with updated props. diff --git a/includes/emails/class-wc-email-customer-invoice.php b/includes/emails/class-wc-email-customer-invoice.php index 15d32a1d79e..ee332af49b9 100644 --- a/includes/emails/class-wc-email-customer-invoice.php +++ b/includes/emails/class-wc-email-customer-invoice.php @@ -84,12 +84,12 @@ if ( ! class_exists( 'WC_Email_Customer_Invoice', false ) ) : public function get_subject() { if ( $this->object->has_status( array( 'completed', 'processing' ) ) ) { $subject = $this->get_option( 'subject_paid', $this->get_default_subject( true ) ); - $action = 'woocommerce_email_subject_customer_invoice_paid'; - } else { - $subject = $this->get_option( 'subject', $this->get_default_subject() ); - $action = 'woocommerce_email_subject_customer_invoice'; + + return apply_filters( 'woocommerce_email_subject_customer_invoice_paid', $this->format_string( $subject ), $this->object ); } - return apply_filters( $action, $this->format_string( $subject ), $this->object ); + + $subject = $this->get_option( 'subject', $this->get_default_subject() ); + return apply_filters( 'woocommerce_email_subject_customer_invoice', $this->format_string( $subject ), $this->object ); } /** @@ -101,12 +101,11 @@ if ( ! class_exists( 'WC_Email_Customer_Invoice', false ) ) : public function get_heading() { if ( $this->object->has_status( wc_get_is_paid_statuses() ) ) { $heading = $this->get_option( 'heading_paid', $this->get_default_heading( true ) ); - $action = 'woocommerce_email_heading_customer_invoice_paid'; - } else { - $heading = $this->get_option( 'heading', $this->get_default_heading() ); - $action = 'woocommerce_email_heading_customer_invoice'; + return apply_filters( 'woocommerce_email_heading_customer_invoice_paid', $this->format_string( $heading ), $this->object ); } - return apply_filters( $action, $this->format_string( $heading ), $this->object ); + + $heading = $this->get_option( 'heading', $this->get_default_heading() ); + return apply_filters( 'woocommerce_email_heading_customer_invoice', $this->format_string( $heading ), $this->object ); } /** diff --git a/includes/wc-stock-functions.php b/includes/wc-stock-functions.php index fff31281453..9903280971a 100644 --- a/includes/wc-stock-functions.php +++ b/includes/wc-stock-functions.php @@ -46,7 +46,11 @@ function wc_update_product_stock( $product, $stock_quantity = null, $operation = $product_with_stock->set_date_modified( current_time( 'timestamp', true ) ); $product_with_stock->save(); - do_action( $product_with_stock->is_type( 'variation' ) ? 'woocommerce_variation_set_stock' : 'woocommerce_product_set_stock', $product_with_stock ); + if ( $product_with_stock->is_type( 'variation' ) ) { + do_action( 'woocommerce_variation_set_stock', $product_with_stock ); + } else { + do_action( 'woocommerce_product_set_stock', $product_with_stock ); + } return $product_with_stock->get_stock_quantity(); } diff --git a/templates/cart/cart-shipping.php b/templates/cart/cart-shipping.php index 2f8bc3dcbfa..c3a88447883 100644 --- a/templates/cart/cart-shipping.php +++ b/templates/cart/cart-shipping.php @@ -45,7 +45,13 @@ if ( ! defined( 'ABSPATH' ) ) { do_action( 'woocommerce_after_shipping_rate', $method, $index ); ?> customer->has_calculated_shipping() ) : ?> - +
HookTypeFile(s)