0

Move manual fix applied to plugin source code to theme functions.php or as Code Snippet

I've applied a manual fix to a plugin file.

I'd like to make this fix more accessible and scalable by hooking this through the theme's functions.php or adding it as a snippet using a plugin like Code Snippets.

The Manual Fix applied to plugin file (functions as expected):

/**
         * Add admin meta box with actions
         *
         * @access public
         * @return void
         */
        public function add_woo_pdf_metabox()
        {
            global $post;

            if (!$post) {
                return;
            }

            /* Manual Fix Applied */
            $post_type_order = get_post_type( $post );
            echo $post_type_order;

            if ($post_type_order != 'shop_order') {
                return;
            }
            /* End Manual Fix */

            $order = new WC_Order($post->ID);

            if (!$order) {
                return;
            }

            $invoice = $this->get_invoice($post->ID);

            if ((is_array($invoice) && !empty($invoice) && $this->opt['woo_pdf_enabled']) || (!is_array($invoice) && $this->opt['woo_pdf_proforma_enabled'] && ($order->status != 'completed' || $this->opt['woo_pdf_enabled'])) || ($this->opt['woo_pdf_enabled'])) {
                add_meta_box('woo_pdf_metabox', __('PDF Invoices', 'woo_pdf'), array($this, 'woo_pdf_metabox_content'), 'shop_order', 'side', 'default');
            }
        }

Adjusted fix saved as Code Snippet using keyword extends on plugin Class (does not function):

add_action('plugins_loaded', 'regulate_pdf_metabox_appearance'); // hook used to call function

function regulate_pdf_metabox_appearance() { // wrapped in function
  if (class_exists('WooPDF')) {

    class WooPDF_custom extends WooPDF {

      /**
      * Add admin meta box with actions
      *
      * @access public
      * @return void
      */
        function add_woo_pdf_metabox() {
          global $post;

          if (!$post) {
            return;
          }

          /* Manual Fix Applied */
          $post_type_order = get_post_type( $post );

          if ($post_type_order != 'shop_order') {
            return;
          }

          $order = new WC_Order($post->ID);

          if (!$order) {
            return;
          }

          $invoice = $this->get_invoice($post->ID);

          if ((is_array($invoice) && !empty($invoice) && $this->opt['woo_pdf_enabled']) || (!is_array($invoice) && $this->opt['woo_pdf_proforma_enabled'] && ($order->status != 'completed' || $this->opt['woo_pdf_enabled'])) || ($this->opt['woo_pdf_enabled'])) {
            add_meta_box('woo_pdf_metabox', __('PDF Invoices', 'woo_pdf'), array($this, 'woo_pdf_metabox_content'), 'shop_order', 'side', 'default');
          }
        }
    }

  }
}

What is required in the code above to have the same fix work using extends and inserting it into the theme's functions.php or through a Code Snippet?

  • How are you using it? Because if you extend a class you have to call the name of the extended class to function as you expected. More info here php.net/manual/en/language.oop5.inheritance.php – Drupalizeme Nov 13 '17 at 10:17
  • Referring to this answer: wordpress.stackexchange.com/questions/110672/… I've since wrapped it in a function. I am now using plugins_loaded hook to call it (question updated) - which I had assumed would call the name of the extended class. This is not working either. Should I be using another hook or filter? – UncaughtTypeError Nov 13 '17 at 12:07
  • This answer just create a new object of the WC_Coupon. – Drupalizeme Nov 13 '17 at 12:58
  • Thanks for pointing that out, but it doesn't address my concern. Do you know of a solution that can address the issue I've demonstrated in the scope of wordpress? A solution pertaining to php in general will be too broad in scope for me unfortunately, since my experience with php is far too limited to put any of that into context without any sort of guidance, and although links to documentation are often helpful, if I have no frame of reference, I struggle to find it very useful. – UncaughtTypeError Nov 13 '17 at 13:31
  • The WP way to extend and modify are actions and filters. So what you are doing can only work for code calling the WooPDF_custom. – Drupalizeme Nov 13 '17 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.