Skip to content

JavaScript Handling

Deiva Magalhaes edited this page May 5, 2020 · 10 revisions

A new SkyVerge\WooCommerce\PluginFramework\vX_Y_Z\Frontend\Script_Handler abstract class was added in version 5.7.0. The existing payment gateways frontend handler classes (SV_WC_Payment_Gateway_Payment_Form, SV_WC_Payment_Gateway_My_Payment_Methods and SV_WC_Payment_Gateway_Apple_Pay_Frontend) now extend this class.

PHP Handler Changes

Any custom handler enqueuing a JavaScript class initialization should:

  • Extend (directly or indirectly) the Script_Handler class
  • Define a $js_handler_base_class_name protected attribute, containing the JavaScript handler base class name, without the framework version namespace, e.g., $this->js_handler_base_class_name = 'SV_WC_Payment_Form_Handler';
  • Deprecate its own get_js_handler_name() method, if it has one, and replace calls to it by calls to get_js_handler_class_name()
  • Define a get_id() and optionally a get_id_dasherized() methods to return the ID of your choosing, like a gateway or plugin ID
  • Define the log_event() method
  • Define the is_logging_enabled() method, if needed
  • If a constructor is defined, be sure to call parent::__construct()
  • If an add_hooks() method is defined, be sure to call parent::add_hooks()
  • In the enqueued JavaScript:
    • Define a load function, instantiating the JavaScript class returned by get_js_handler_class_name() with the args returned by get_js_handler_args()
    • In a try/catch block, check if the class is available and call the load function
    • If the class is not available and in the corresponding catch block:
      • Add a listener to the corresponding JavaScript event, returned by get_js_loaded_event()
      • Echo $this->get_js_handler_event_debug_log_request(); to echo the script that makes the AJAX call to log errors initializing JavaScript handlers

See SV_WC_Payment_Gateway_Payment_Form::render_js() for an example.

JavaScript Handler Changes

Any custom JavaScript handler class should:

  • Be namespaced with the framework version, e.g., SV_WC_Payment_Form_Handler_v5_7_0
  • After the class definition, dispatch a JavaScript event, e.g., $( document.body ).trigger( "sv_wc_payment_form_handler_v5_7_0_loaded" )

See SV_WC_Payment_Form_Handler_vX_Y_Z for an example.