O
O
OneTwoThreeFourFive2020-06-11 21:02:27
WordPress
OneTwoThreeFourFive, 2020-06-11 21:02:27

Which design pattern is suitable for a WordPress plugin?

The plugin adds widgets to Elementor, product settings to WooCommerce, uses various hooks from WooCommerce and other plugins. Now I just created a common class and I include files in this class. For example, to change the fields on the checkout page, I created a file in the plugin called Checkout.php and created a class. That is, this is not OOP at all, but ordinary functional code, just in a class. Also, the plugin must connect to another server via the API. How to organize all this correctly? I read about MVC. Does it fit here? What will be the model, view, controller? I realized that viewing is template files with HTML markup, is it possible to connect a file or should it be a method in which a file is connected, which must be called in another class?

class Checkout {
  public function __construct() {
    add_filter( 'woocommerce_checkout_fields', array( $this, 'checkout_fields' ), 10, 1 );
    add_filter( 'woocommerce_checkout_fields', array( $this, 'checkout_remove_fields' ), 10, 1 );
  }

  public function checkout_fields( $fields ) {
    $fields['billing']['billing_first_name']['placeholder'] = esc_html__( 'First Name*', 'textdomain' );
    $fields['billing']['billing_first_name']['label'] = esc_html__( 'First Name', 'textdomain' );
    $fields['billing']['billing_first_name']['priority'] = 5;

    $fields['billing']['billing_last_name']['placeholder'] = esc_html__( 'Last Name*', 'textdomain' );
    $fields['billing']['billing_last_name']['label'] = esc_html__( 'Last Name', 'textdomain' );
    $fields['billing']['billing_last_name']['priority'] = 10;

    $fields['billing']['billing_address_1']['placeholder'] = esc_html__( 'Address (optional)', 'textdomain' );
    $fields['billing']['billing_address_1']['label'] = esc_html__( 'Address', 'textdomain' );
    $fields['billing']['billing_address_1']['required'] = false;
    $fields['billing']['billing_address_1']['priority'] = 15;

    $fields['billing']['billing_email']['placeholder'] = esc_html__( 'Email Address*', 'textdomain' );
    $fields['billing']['billing_email']['label'] = esc_html__( 'Email Address', 'textdomain' );
    $fields['billing']['billing_email']['priority'] = 20;

    $fields['account']['account_password']['label'] = esc_html__( 'Password', 'textdomain' );
    $fields['account']['account_password']['placeholder'] = esc_html__( 'Password*', 'textdomain' );

    return $fields;
  }

  public function checkout_remove_fields( $fields ) {
    unset(
      $fields['billing']['billing_address_2'],
      $fields['billing']['billing_phone'],
      $fields['billing']['billing_company'],
      $fields['billing']['billing_city'],
      $fields['billing']['billing_postcode'],
      $fields['billing']['billing_state'],
      $fields['billing']['billing_country']
    );

    return $fields;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2020-06-11
@glaphire

The usual refactoring at the level of "readable" and "without duplication" is enough (with smearing into a couple of classes if necessary), MVC and patterns are not the answer to such a question (MVC - because this is a generalized concept and is simply pulled to the web from another topic, but the pattern can appear itself during refactoring, and the boundaries between some of them are conditional).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question