*/ namespace StorePlugin\CustomOrderStatus\Abstracts; /** * AbstractMetaBoxes * * WooCommerce custom order status post type metabox */ abstract class AbstractMetaBoxes { /** * Register with php-di * * @return void */ public function register(): void { \add_action('add_meta_boxes', [$this, 'registerMetabox']); \add_action('save_post', [$this, 'saveOrderStatuses']); \add_action('post_edit_form_tag', [$this, 'update_edit_form']); } /** * Order status slug filed * * @return void */ public function registerMetabox() { \add_meta_box( $this->ID(), $this->title(), [$this, 'executeFields'], $this->screen(), $this->context(), $this->priority(), ); } /** * Set Metabox ID * * @return int */ abstract protected function ID(): string; /** * Set Metabox title * * @return string */ abstract protected function title(): string; /** * Set Metabox screen like post type, comment etc * * @return string */ abstract protected function screen(): string; /** * Add Meta Context * Side, Normal, Advanced * * @return string */ protected function context(): string { return 'normal'; } /** * Add Meta Priority like high * * @return string */ protected function priority(): string { return ''; } /** * Add Meta Fields * * @return array */ abstract protected function addFields($prefix = ''); /** * Set form type * * @return void */ function update_edit_form() { echo ' enctype="multipart/form-data"'; } /** * Get trimmed and formated slug * * @param mixed $slug * @return string */ public function trimmedSlug($slug) { $trimed = trim(substr($slug, 0, 17), ' '); $formated_slug = str_replace(' ', '-', $trimed); return strtolower($formated_slug); } /** * Check whether the order status is default * * @return array */ protected function isDefaultStatus( $post_id ) { $value = get_post_meta( $post_id, "_order_status_default", true ); return ( $value === '' ? true : false ); } /** * Execute Meta fields * * @return void */ public function executeFields($post) { // Get all fields data $metaFields = $this->addFields(); // Enqueue WooCommerce style file only for this page. wp_enqueue_style('wc-admin-app'); wp_enqueue_style('wc-onboarding'); // _wp_nonce for secuirity verification wp_nonce_field('order_status_actions', '_wpnonce_order_status'); echo '
| ' . ( isset( $field['pro-label'] ) ? ' Pro ' : '' ) . ' | ',
'';
switch ($field['type']) {
case 'button':
echo '';
break;
case 'text':
echo '';
if ( ! empty( $field['tip-desc'] ) ) {
echo '';
}
echo ' ', esc_html( $field['desc'] ), ' '; break; case 'email': echo ''; if ( ! empty( $field['tip-desc'] ) ) { echo ''; } echo '', esc_html( $field['desc'] ), ' '; break; case 'slug': if( ! $this->isDefaultStatus( $post->ID ) ) { echo ''; echo '', esc_html( 'You should never change the default order status slug' ), ' '; break; } echo ''; if ( ! empty( $field['tip-desc'] ) ) { echo ''; } echo '', esc_html($field['desc']), ' '; break; case 'textarea': echo ''; if ( ! empty( $field['tip-desc'] ) ) { echo ''; } echo '', esc_html($field['desc'] ), ' '; break; case 'wp_editor': wp_editor( $value, $field['id'], [ 'wpautop' => true, 'media_buttons' => false, 'textarea_name' => $field['id'], 'textarea_rows' => get_option('default_post_edit_rows', 10), 'teeny' => false ]); if ( ! empty( $field['desc'] ) ) { echo '', esc_html( $field['desc'] ), ' '; } break; case 'select': echo ''; break; case 'multiselect': echo ''; break; case 'radio': foreach ( $field['options'] as $option ) { echo '', esc_attr( $option['name'] ); } break; case 'checkbox2': echo ''; break; case 'checkbox': // Toggle switch for Yes/No echo ''; break; case 'color2': echo ''; echo ''; break; case 'color': echo ''; echo ''; echo ''; if ( ! empty( $field['tip-desc'] ) ) { echo ''; } break; case 'icon': do_action( 'wc_custom_order_status_icon', $field['id'], $value ); break; case 'hidden': if( $this->isDefaultStatus( $post->ID ) ) { echo ''; } else { echo ''; } break; } echo ' | ', ' |
|---|