Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -12,8 +12,8 @@ namespace WP_Ultimo\Admin_Pages;
// Exit if accessed directly
defined('ABSPATH') || exit;
use \WP_Ultimo\Models\Discount_Code;
use \WP_Ultimo\Managers\Discount_Code_Manager;
use WP_Ultimo\Models\Discount_Code;
use WP_Ultimo\Managers\Discount_Code_Manager;
/**
* WP Multisite WaaS Discount_Code Edit/Add New Admin Page.
@ -91,21 +91,24 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
parent::register_widgets();
$this->add_fields_widget('description', array(
'title' => __('Description', 'wp-ultimo'),
'position' => 'normal',
'fields' => array(
'description' => array(
'type' => 'textarea',
'title' => __('Description', 'wp-ultimo'),
'placeholder' => __('Tell your customers what this product is about.', 'wp-ultimo'),
'value' => $this->get_object()->get_description(),
'html_attr' => array(
'rows' => 3,
$this->add_fields_widget(
'description',
array(
'title' => __('Description', 'wp-ultimo'),
'position' => 'normal',
'fields' => array(
'description' => array(
'type' => 'textarea',
'title' => __('Description', 'wp-ultimo'),
'placeholder' => __('Tell your customers what this product is about.', 'wp-ultimo'),
'value' => $this->get_object()->get_description(),
'html_attr' => array(
'rows' => 3,
),
),
),
),
));
)
);
$tz_note = sprintf('The site timezone is <code>%s</code>. The current time is <code>%s</code>', date_i18n('e'), date_i18n('r'));
@ -215,161 +218,175 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
'v-model' => 'limit_products',
),
),
), $this->get_product_field_list()
),
$this->get_product_field_list()
),
),
);
$this->add_tabs_widget('options', array(
'title' => __('Advanced Options', 'wp-ultimo'),
'position' => 'normal',
'sections' => apply_filters('wu_discount_code_options_sections', $options, $this->get_object()),
));
$this->add_tabs_widget(
'options',
array(
'title' => __('Advanced Options', 'wp-ultimo'),
'position' => 'normal',
'sections' => apply_filters('wu_discount_code_options_sections', $options, $this->get_object()),
)
);
/*
* Handle legacy options for back-compat.
*/
$this->handle_legacy_options();
$this->add_list_table_widget('events', array(
'title' => __('Events', 'wp-ultimo'),
'table' => new \WP_Ultimo\List_Tables\Inside_Events_List_Table(),
'query_filter' => array($this, 'query_filter'),
));
$this->add_list_table_widget(
'events',
array(
'title' => __('Events', 'wp-ultimo'),
'table' => new \WP_Ultimo\List_Tables\Inside_Events_List_Table(),
'query_filter' => array($this, 'query_filter'),
)
);
$this->add_save_widget('save', array(
'html_attr' => array(
'data-wu-app' => 'save_discount_code',
'data-state' => wu_convert_to_state(array(
'apply_to_setup_fee' => $this->get_object()->get_setup_fee_value() > 0,
'code' => $this->get_object()->get_code(),
'type' => $this->get_object()->get_type(),
'value' => $this->get_object()->get_value(),
'setup_fee_type' => $this->get_object()->get_setup_fee_type(),
'setup_fee_value' => $this->get_object()->get_setup_fee_value(),
)),
),
'fields' => array(
'code' => array(
'title' => __('Coupon Code', 'wp-ultimo'),
'type' => 'text',
'placeholder' => __('E.g. XMAS10OFF', 'wp-ultimo'),
'desc' => __('The actual code your customers will enter during checkout.', 'wp-ultimo'),
'value' => $this->get_object()->get_code(),
'tooltip' => '',
'wrapper_html_attr' => array(
'v-cloak' => '1',
),
'html_attr' => array(
'v-on:input' => 'code = $event.target.value.toUpperCase().replace(/[^A-Z0-9-_]+/g, "")',
'v-bind:value' => 'code',
$this->add_save_widget(
'save',
array(
'html_attr' => array(
'data-wu-app' => 'save_discount_code',
'data-state' => wu_convert_to_state(
array(
'apply_to_setup_fee' => $this->get_object()->get_setup_fee_value() > 0,
'code' => $this->get_object()->get_code(),
'type' => $this->get_object()->get_type(),
'value' => $this->get_object()->get_value(),
'setup_fee_type' => $this->get_object()->get_setup_fee_type(),
'setup_fee_value' => $this->get_object()->get_setup_fee_value(),
)
),
),
'value_group' => array(
'type' => 'group',
'title' => __('Discount', 'wp-ultimo'),
'wrapper_html_attr' => array(
'v-cloak' => '1',
'fields' => array(
'code' => array(
'title' => __('Coupon Code', 'wp-ultimo'),
'type' => 'text',
'placeholder' => __('E.g. XMAS10OFF', 'wp-ultimo'),
'desc' => __('The actual code your customers will enter during checkout.', 'wp-ultimo'),
'value' => $this->get_object()->get_code(),
'tooltip' => '',
'wrapper_html_attr' => array(
'v-cloak' => '1',
),
'html_attr' => array(
'v-on:input' => 'code = $event.target.value.toUpperCase().replace(/[^A-Z0-9-_]+/g, "")',
'v-bind:value' => 'code',
),
),
'fields' => array(
'type' => array(
'type' => 'select',
'value' => $this->get_object()->get_type(),
'placeholder' => '',
'wrapper_classes' => 'wu-w-2/3',
'options' => array(
'percentage' => __('Percentage (%)', 'wp-ultimo'),
// translators: %s is the currency symbol. e.g. $
'absolute' => sprintf(__('Absolute (%s)', 'wp-ultimo'), wu_get_currency_symbol()),
'value_group' => array(
'type' => 'group',
'title' => __('Discount', 'wp-ultimo'),
'wrapper_html_attr' => array(
'v-cloak' => '1',
),
'fields' => array(
'type' => array(
'type' => 'select',
'value' => $this->get_object()->get_type(),
'placeholder' => '',
'wrapper_classes' => 'wu-w-2/3',
'options' => array(
'percentage' => __('Percentage (%)', 'wp-ultimo'),
// translators: %s is the currency symbol. e.g. $
'absolute' => sprintf(__('Absolute (%s)', 'wp-ultimo'), wu_get_currency_symbol()),
),
'html_attr' => array(
'v-model' => 'type',
),
),
'html_attr' => array(
'v-model' => 'type',
'value' => array(
'type' => 'number',
'value' => $this->get_object()->get_value(),
'placeholder' => '',
'wrapper_classes' => 'wu-ml-2 wu-w-1/3',
'html_attr' => array(
'min' => 0,
'v-bind:max' => "type === 'percentage' ? 100 : 999999999",
'step' => 'any',
),
),
),
'value' => array(
'type' => 'number',
'value' => $this->get_object()->get_value(),
'placeholder' => '',
'wrapper_classes' => 'wu-ml-2 wu-w-1/3',
'html_attr' => array(
'min' => 0,
'v-bind:max' => "type === 'percentage' ? 100 : 999999999",
'step' => 'any'
),
'apply_to_renewals' => array(
'type' => 'toggle',
'title' => __('Apply to Renewals', 'wp-ultimo'),
'desc' => __('By default, discounts are only applied to the first payment.', 'wp-ultimo'),
'value' => $this->get_object()->should_apply_to_renewals(),
'wrapper_html_attr' => array(
'v-cloak' => '1',
),
),
'apply_to_setup_fee' => array(
'type' => 'toggle',
'title' => __('Setup Fee Discount', 'wp-ultimo'),
'desc' => __('Also set a discount for setup fee?', 'wp-ultimo'),
'value' => $this->get_object()->get_setup_fee_value() > 0,
'html_attr' => array(
'v-model' => 'apply_to_setup_fee',
),
'wrapper_html_attr' => array(
'v-cloak' => '1',
),
),
'setup_fee_value_group' => array(
'type' => 'group',
'title' => __('Setup Fee Discount', 'wp-ultimo'),
'wrapper_html_attr' => array(
'v-show' => 'apply_to_setup_fee',
'v-cloak' => '1',
),
'fields' => array(
'setup_fee_type' => array(
'type' => 'select',
'value' => $this->get_object()->get_setup_fee_type(),
'placeholder' => '',
'wrapper_classes' => 'wu-w-2/3',
'options' => array(
'percentage' => __('Percentage (%)', 'wp-ultimo'),
// translators: %s is the currency symbol. e.g $
'absolute' => sprintf(__('Absolute (%s)', 'wp-ultimo'), wu_get_currency_symbol()),
),
'html_attr' => array(
'v-model' => 'setup_fee_type',
),
),
'setup_fee_value' => array(
'type' => 'number',
'value' => $this->get_object()->get_setup_fee_value(),
'placeholder' => '',
'wrapper_classes' => 'wu-ml-2 wu-w-1/3',
'html_attr' => array(
'min' => 0,
'v-bind:max' => "setup_fee_type === 'percentage' ? 100 : 999999999",
),
),
),
),
),
'apply_to_renewals' => array(
'type' => 'toggle',
'title' => __('Apply to Renewals', 'wp-ultimo'),
'desc' => __('By default, discounts are only applied to the first payment.', 'wp-ultimo'),
'value' => $this->get_object()->should_apply_to_renewals(),
'wrapper_html_attr' => array(
'v-cloak' => '1',
),
),
'apply_to_setup_fee' => array(
'type' => 'toggle',
'title' => __('Setup Fee Discount', 'wp-ultimo'),
'desc' => __('Also set a discount for setup fee?', 'wp-ultimo'),
'value' => $this->get_object()->get_setup_fee_value() > 0,
'html_attr' => array(
'v-model' => 'apply_to_setup_fee',
),
'wrapper_html_attr' => array(
'v-cloak' => '1',
),
),
'setup_fee_value_group' => array(
'type' => 'group',
'title' => __('Setup Fee Discount', 'wp-ultimo'),
'wrapper_html_attr' => array(
'v-show' => 'apply_to_setup_fee',
'v-cloak' => '1',
),
'fields' => array(
'setup_fee_type' => array(
'type' => 'select',
'value' => $this->get_object()->get_setup_fee_type(),
'placeholder' => '',
'wrapper_classes' => 'wu-w-2/3',
'options' => array(
'percentage' => __('Percentage (%)', 'wp-ultimo'),
// translators: %s is the currency symbol. e.g $
'absolute' => sprintf(__('Absolute (%s)', 'wp-ultimo'), wu_get_currency_symbol()),
),
'html_attr' => array(
'v-model' => 'setup_fee_type',
),
),
'setup_fee_value' => array(
'type' => 'number',
'value' => $this->get_object()->get_setup_fee_value(),
'placeholder' => '',
'wrapper_classes' => 'wu-ml-2 wu-w-1/3',
'html_attr' => array(
'min' => 0,
'v-bind:max' => "setup_fee_type === 'percentage' ? 100 : 999999999",
),
),
),
),
),
));
)
);
$this->add_fields_widget('active', array(
'title' => __('Active', 'wp-ultimo'),
'fields' => array(
'active' => array(
'type' => 'toggle',
'title' => __('Active', 'wp-ultimo'),
'desc' => __('Use this option to manually enable or disable this discount code for new sign-ups.', 'wp-ultimo'),
'value' => $this->get_object()->is_active(),
$this->add_fields_widget(
'active',
array(
'title' => __('Active', 'wp-ultimo'),
'fields' => array(
'active' => array(
'type' => 'toggle',
'title' => __('Active', 'wp-ultimo'),
'desc' => __('Use this option to manually enable or disable this discount code for new sign-ups.', 'wp-ultimo'),
'value' => $this->get_object()->is_active(),
),
),
),
));
} // end register_widgets;
)
);
}
/**
* List of products to apply this coupon to.
@ -382,10 +399,9 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
$fields = array();
foreach (wu_get_products() as $product) {
$product_id = $product->get_id();
$fields["allowed_products_{$product_id}"] = array(
$fields[ "allowed_products_{$product_id}" ] = array(
'type' => 'toggle',
'title' => $product->get_name(),
'desc' => __('Make applicable to this product.', 'wp-ultimo'),
@ -410,11 +426,9 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
':name' => "'allowed_products[]'",
),
);
} // end foreach;
}
if (empty($fields)) {
$fields['allowed_products_no_products'] = array(
'type' => 'note',
'title' => '',
@ -424,12 +438,10 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
'v-show' => 'limit_products',
),
);
} // end if;
}
return $fields;
} // end get_product_field_list;
}
/**
* Handles legacy advanced options for coupons.
@ -443,11 +455,9 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
$tabs = array(__('Legacy Add-ons', 'wp-ultimo'));
if (!isset($wp_filter['wp_ultimo_coupon_advanced_options'])) {
if ( ! isset($wp_filter['wp_ultimo_coupon_advanced_options'])) {
return;
} // end if;
}
wp_enqueue_style('wu-legacy-admin-tabs', wu_get_asset('legacy-admin-tabs.css', 'css'), false, wu_get_version());
@ -463,35 +473,32 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
);
foreach ($priorities as $priority => $callbacks) {
foreach ($callbacks as $id => $callable) {
$fields[$id] = array(
$fields[ $id ] = array(
'type' => 'html',
'classes' => 'wu--mt-2',
'content' => function() use ($callable) {
'content' => function () use ($callable) {
call_user_func($callable['function'], $this->get_object());
},
);
}
}
} // end foreach;
} // end foreach;
$this->add_fields_widget('legacy-options', array(
'title' => __('Legacy Options', 'wp-ultimo'),
'position' => 'normal',
'fields' => $fields,
'classes' => 'wu-legacy-options-panel',
'field_wrapper_classes' => 'wu-w-full wu-box-border wu-items-center wu-flex wu-justify-between wu-p-4 wu-m-0 wu-border-t wu-border-l-0 wu-border-r-0 wu-border-b-0 wu-border-gray-300 wu-border-solid',
'html_attr' => array(
'style' => 'margin-top: -5px;',
),
));
} // end handle_legacy_options;
$this->add_fields_widget(
'legacy-options',
array(
'title' => __('Legacy Options', 'wp-ultimo'),
'position' => 'normal',
'fields' => $fields,
'classes' => 'wu-legacy-options-panel',
'field_wrapper_classes' => 'wu-w-full wu-box-border wu-items-center wu-flex wu-justify-between wu-p-4 wu-m-0 wu-border-t wu-border-l-0 wu-border-r-0 wu-border-b-0 wu-border-gray-300 wu-border-solid',
'html_attr' => array(
'style' => 'margin-top: -5px;',
),
)
);
}
/**
* Register ajax forms that we use for discount code.
@ -504,11 +511,13 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
* Delete Discount code - Confirmation modal
*/
add_filter('wu_data_json_success_delete_discount_code_modal', fn($data_json) => array(
'redirect_url' => wu_network_admin_url('wp-ultimo-discount-codes', array('deleted' => 1))
));
} // end register_forms;
add_filter(
'wu_data_json_success_delete_discount_code_modal',
fn($data_json) => array(
'redirect_url' => wu_network_admin_url('wp-ultimo-discount-codes', array('deleted' => 1)),
)
);
}
/**
* Filters the list table to return only relevant events.
@ -526,8 +535,7 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
);
return array_merge($args, $extra_args);
} // end query_filter;
}
/**
* Returns the title of the page.
@ -538,8 +546,7 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
public function get_title() {
return $this->edit ? __('Edit Discount Code', 'wp-ultimo') : __('Add new Discount Code', 'wp-ultimo');
} // end get_title;
}
/**
* Returns the title of menu for this page.
@ -550,8 +557,7 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
public function get_menu_title() {
return __('Edit Discount Code', 'wp-ultimo');
} // end get_menu_title;
}
/**
* Returns the action links for that page.
@ -562,8 +568,7 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
public function action_links() {
return array();
} // end action_links;
}
/**
* Returns the labels to be used on the admin page.
@ -584,8 +589,7 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
'delete_button_label' => __('Delete Discount Code', 'wp-ultimo'),
'delete_description' => __('Be careful. This action is irreversible.', 'wp-ultimo'),
);
} // end get_labels;
}
/**
* Returns the object being edit at the moment.
@ -596,36 +600,29 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
public function get_object() {
if ($this->object !== null) {
return $this->object;
} // end if;
}
if (isset($_GET['id'])) {
$item_id = wu_request('id', 0);
$item = wu_get_discount_code($item_id);
if (!$item) {
if ( ! $item) {
wp_redirect(wu_network_admin_url('wp-ultimo-discount_codes'));
exit;
} // end if;
}
$this->object = $item;
return $this->object;
}
} // end if;
$this->object = new Discount_Code;
$this->object = new Discount_Code();
return $this->object;
} // end get_object;
}
/**
* Discount_Codes have titles.
*
@ -634,8 +631,7 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
public function has_title(): bool {
return true;
} // end has_title;
}
/**
* Should implement the processes necessary to save the changes made to the object.
@ -647,49 +643,37 @@ class Discount_Code_Edit_Admin_Page extends Edit_Admin_Page {
/*
* Set the recurring value to zero if the toggle is disabled.
*/
if (!wu_request('apply_to_renewals')) {
if ( ! wu_request('apply_to_renewals')) {
$_POST['apply_to_renewals'] = false;
} // end if;
}
/*
* Set the limit products value.
*/
if (!wu_request('limit_products')) {
if ( ! wu_request('limit_products')) {
$_POST['limit_products'] = false;
} // end if;
}
/*
* Set the setup fee value to zero if the toggle is disabled.
*/
if (!wu_request('apply_to_setup_fee')) {
if ( ! wu_request('apply_to_setup_fee')) {
$_POST['setup_fee_value'] = 0;
} // end if;
}
/**
* Unset dates to prevent invalid dates
*/
if (!wu_request('enable_date_start') || !wu_validate_date(wu_request('date_start'))) {
if ( ! wu_request('enable_date_start') || ! wu_validate_date(wu_request('date_start'))) {
$_POST['date_start'] = null;
}
} // end if;
if (!wu_request('enable_date_expiration') || !wu_validate_date(wu_request('date_expiration'))) {
if ( ! wu_request('enable_date_expiration') || ! wu_validate_date(wu_request('date_expiration'))) {
$_POST['date_expiration'] = null;
} // end if;
}
$_POST['code'] = trim((string) wu_request('code'));
parent::handle_save();
} // end handle_save;
} // end class Discount_Code_Edit_Admin_Page;
}
}