Use new code style
This commit is contained in:
@ -50,8 +50,7 @@ class Form_Manager extends Base_Manager {
|
||||
add_action('wu_page_load', 'add_wubox');
|
||||
|
||||
do_action('wu_register_forms');
|
||||
|
||||
} // end init;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the form unavailable message.
|
||||
@ -68,25 +67,25 @@ class Form_Manager extends Base_Manager {
|
||||
$message = __('Form not available', 'wp-ultimo');
|
||||
|
||||
if (is_wp_error($error)) {
|
||||
|
||||
$message = $error->get_error_message();
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
echo sprintf('
|
||||
printf(
|
||||
'
|
||||
<div class="wu-modal-form wu-h-full wu-flex wu-items-center wu-justify-center wu-bg-gray-200 wu-m-0 wu-mt-0 wu--mb-3">
|
||||
<div>
|
||||
<span class="dashicons dashicons-warning wu-h-8 wu-w-8 wu-mx-auto wu-text-center wu-text-4xl wu-block"></span>
|
||||
<span class="wu-block wu-text-sm">%s</span>
|
||||
</div>
|
||||
</div>
|
||||
', $message);
|
||||
',
|
||||
$message
|
||||
);
|
||||
|
||||
do_action('wu_form_scripts', false);
|
||||
|
||||
die;
|
||||
|
||||
} // end display_form_unavailable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a registered form, when requested.
|
||||
@ -100,18 +99,27 @@ class Form_Manager extends Base_Manager {
|
||||
|
||||
$form = $this->get_form(wu_request('form'));
|
||||
|
||||
echo sprintf("<form class='wu_form wu-styling' id='%s' action='%s' method='post'>",
|
||||
$form['id'],
|
||||
$this->get_form_url($form['id'], array(
|
||||
'action' => 'wu_form_handler',
|
||||
)));
|
||||
printf(
|
||||
"<form class='wu_form wu-styling' id='%s' action='%s' method='post'>",
|
||||
$form['id'],
|
||||
$this->get_form_url(
|
||||
$form['id'],
|
||||
array(
|
||||
'action' => 'wu_form_handler',
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
echo sprintf('
|
||||
printf(
|
||||
'
|
||||
<div v-cloak data-wu-app="%s" data-state="%s">
|
||||
<ul class="wu-p-4 wu-bg-red-200 wu-m-0 wu-list-none" v-if="errors.length">
|
||||
<li class="wu-m-0 wu-p-0" v-for="error in errors">{{ error.message }}</li>
|
||||
</ul>
|
||||
</div>', $form['id'] . '_errors', htmlspecialchars(json_encode(array('errors' => array()))));
|
||||
</div>',
|
||||
$form['id'] . '_errors',
|
||||
htmlspecialchars(json_encode(array('errors' => array())))
|
||||
);
|
||||
|
||||
call_user_func($form['render']);
|
||||
|
||||
@ -124,8 +132,7 @@ class Form_Manager extends Base_Manager {
|
||||
do_action('wu_form_scripts', $form);
|
||||
|
||||
exit;
|
||||
|
||||
} // end display_form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the submission of a registered form.
|
||||
@ -139,11 +146,9 @@ class Form_Manager extends Base_Manager {
|
||||
|
||||
$form = $this->get_form(wu_request('form'));
|
||||
|
||||
if (!wp_verify_nonce(wu_request('_wpnonce'), 'wu_form_' . $form['id'])) {
|
||||
|
||||
if ( ! wp_verify_nonce(wu_request('_wpnonce'), 'wu_form_' . $form['id'])) {
|
||||
wp_send_json_error();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/**
|
||||
* The handler is supposed to send a wp_json message back.
|
||||
@ -153,14 +158,11 @@ class Form_Manager extends Base_Manager {
|
||||
$check = call_user_func($form['handler']);
|
||||
|
||||
if (is_wp_error($check)) {
|
||||
|
||||
$this->display_form_unavailable($check);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
exit;
|
||||
|
||||
} // end handle_form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the form exists and that the user has permission to see it.
|
||||
@ -173,26 +175,19 @@ class Form_Manager extends Base_Manager {
|
||||
* We only want ajax requests.
|
||||
*/
|
||||
if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower((string) $_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')) {
|
||||
|
||||
wp_die(0);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$form = $this->get_form(wu_request('form'));
|
||||
|
||||
if (!$form) {
|
||||
|
||||
if ( ! $form) {
|
||||
return $this->display_form_unavailable();
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
if (!current_user_can($form['capability'])) {
|
||||
|
||||
if ( ! current_user_can($form['capability'])) {
|
||||
return $this->display_form_unavailable();
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end security_checks;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all the registered gateways.
|
||||
@ -203,8 +198,7 @@ class Form_Manager extends Base_Manager {
|
||||
public function get_registered_forms() {
|
||||
|
||||
return $this->registered_forms;
|
||||
|
||||
} // end get_registered_forms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a form is already registered.
|
||||
@ -216,9 +210,8 @@ class Form_Manager extends Base_Manager {
|
||||
*/
|
||||
public function is_form_registered($id) {
|
||||
|
||||
return is_array($this->registered_forms) && isset($this->registered_forms[$id]);
|
||||
|
||||
} // end is_form_registered;
|
||||
return is_array($this->registered_forms) && isset($this->registered_forms[ $id ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a registered form.
|
||||
@ -230,9 +223,8 @@ class Form_Manager extends Base_Manager {
|
||||
*/
|
||||
public function get_form($id) {
|
||||
|
||||
return $this->is_form_registered($id) ? $this->registered_forms[$id] : false;
|
||||
|
||||
} // end get_form;
|
||||
return $this->is_form_registered($id) ? $this->registered_forms[ $id ] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a new Ajax Form.
|
||||
@ -249,26 +241,26 @@ class Form_Manager extends Base_Manager {
|
||||
*/
|
||||
public function register_form($id, $atts = array()) {
|
||||
|
||||
$atts = wp_parse_args($atts, array(
|
||||
'id' => $id,
|
||||
'form' => '',
|
||||
'capability' => 'manage_network',
|
||||
'handler' => '__return_false',
|
||||
'render' => '__return_empty_string',
|
||||
));
|
||||
$atts = wp_parse_args(
|
||||
$atts,
|
||||
array(
|
||||
'id' => $id,
|
||||
'form' => '',
|
||||
'capability' => 'manage_network',
|
||||
'handler' => '__return_false',
|
||||
'render' => '__return_empty_string',
|
||||
)
|
||||
);
|
||||
|
||||
// Checks if gateway was already added
|
||||
if ($this->is_form_registered($id)) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
$this->registered_forms[$id] = $atts;
|
||||
$this->registered_forms[ $id ] = $atts;
|
||||
|
||||
return true;
|
||||
|
||||
} // end register_form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ajax URL for a given form.
|
||||
@ -281,16 +273,18 @@ class Form_Manager extends Base_Manager {
|
||||
*/
|
||||
public function get_form_url($form_id, $atts = array()) {
|
||||
|
||||
$atts = wp_parse_args($atts, array(
|
||||
'form' => $form_id,
|
||||
'action' => 'wu_form_display',
|
||||
'width' => '400',
|
||||
'height' => '360',
|
||||
));
|
||||
$atts = wp_parse_args(
|
||||
$atts,
|
||||
array(
|
||||
'form' => $form_id,
|
||||
'action' => 'wu_form_display',
|
||||
'width' => '400',
|
||||
'height' => '360',
|
||||
)
|
||||
);
|
||||
|
||||
return add_query_arg($atts, wu_ajax_url('init'));
|
||||
|
||||
} // end get_form_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the confirmation modal form to delete a customer.
|
||||
@ -301,20 +295,25 @@ class Form_Manager extends Base_Manager {
|
||||
|
||||
$model = wu_request('model');
|
||||
|
||||
wu_register_form('delete_modal', array(
|
||||
'render' => array($this, 'render_model_delete_form'),
|
||||
'handler' => array($this, 'handle_model_delete_form'),
|
||||
'capability' => "wu_delete_{$model}s",
|
||||
));
|
||||
wu_register_form(
|
||||
'delete_modal',
|
||||
array(
|
||||
'render' => array($this, 'render_model_delete_form'),
|
||||
'handler' => array($this, 'handle_model_delete_form'),
|
||||
'capability' => "wu_delete_{$model}s",
|
||||
)
|
||||
);
|
||||
|
||||
wu_register_form('bulk_actions', array(
|
||||
'render' => array($this, 'render_bulk_action_form'),
|
||||
'handler' => array($this, 'handle_bulk_action_form'),
|
||||
));
|
||||
wu_register_form(
|
||||
'bulk_actions',
|
||||
array(
|
||||
'render' => array($this, 'render_bulk_action_form'),
|
||||
'handler' => array($this, 'handle_bulk_action_form'),
|
||||
)
|
||||
);
|
||||
|
||||
add_action('wu_handle_bulk_action_form', array($this, 'default_bulk_action_handler'), 100, 3);
|
||||
|
||||
} // end register_action_forms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the deletion confirmation form.
|
||||
@ -335,34 +334,27 @@ class Form_Manager extends Base_Manager {
|
||||
* Handle metadata elements passed as model
|
||||
*/
|
||||
if (strpos((string) $model, '_meta_') !== false) {
|
||||
|
||||
$elements = explode('_meta_', (string) $model);
|
||||
|
||||
$model = $elements[0];
|
||||
|
||||
$meta_key = $elements[1];
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$object = call_user_func("wu_get_{$model}", $id);
|
||||
|
||||
} catch (\Throwable $exception) {
|
||||
|
||||
// No need to do anything, but cool to stop fatal errors.
|
||||
|
||||
} // end try;
|
||||
}
|
||||
|
||||
$object = apply_filters("wu_delete_form_get_object_{$model}", $object, $id, $model);
|
||||
|
||||
if (!$object) {
|
||||
|
||||
if ( ! $object) {
|
||||
$this->display_form_unavailable(new \WP_Error('not-found', __('Object not found.', 'wp-ultimo')));
|
||||
|
||||
return;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$fields = apply_filters(
|
||||
"wu_form_fields_delete_{$model}_modal",
|
||||
@ -406,28 +398,31 @@ class Form_Manager extends Base_Manager {
|
||||
$object
|
||||
);
|
||||
|
||||
$form_attributes = apply_filters("wu_form_attributes_delete_{$model}_modal", array(
|
||||
'title' => 'Delete',
|
||||
'views' => 'admin-pages/fields',
|
||||
'classes' => 'wu-modal-form wu-widget-list wu-striped wu-m-0 wu-mt-0',
|
||||
'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(
|
||||
'data-wu-app' => 'true',
|
||||
'data-state' => json_encode(array(
|
||||
'confirmed' => false,
|
||||
)),
|
||||
),
|
||||
));
|
||||
$form_attributes = apply_filters(
|
||||
"wu_form_attributes_delete_{$model}_modal",
|
||||
array(
|
||||
'title' => 'Delete',
|
||||
'views' => 'admin-pages/fields',
|
||||
'classes' => 'wu-modal-form wu-widget-list wu-striped wu-m-0 wu-mt-0',
|
||||
'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(
|
||||
'data-wu-app' => 'true',
|
||||
'data-state' => json_encode(
|
||||
array(
|
||||
'confirmed' => false,
|
||||
)
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$form = new \WP_Ultimo\UI\Form('total-actions', $fields, $form_attributes);
|
||||
|
||||
do_action("wu_before_render_delete_{$model}_modal", $form);
|
||||
|
||||
$form->render();
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end render_model_delete_form;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the deletion of customer.
|
||||
@ -454,7 +449,6 @@ class Form_Manager extends Base_Manager {
|
||||
* Handle meta key deletion
|
||||
*/
|
||||
if ($meta_key) {
|
||||
|
||||
$status = delete_metadata('wu_membership', wu_request('id'), 'pending_site');
|
||||
|
||||
$data_json_success = array(
|
||||
@ -464,26 +458,20 @@ class Form_Manager extends Base_Manager {
|
||||
wp_send_json_success($data_json_success);
|
||||
|
||||
exit;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$object = call_user_func("wu_get_{$model}", $id);
|
||||
|
||||
} catch (\Throwable $exception) {
|
||||
|
||||
// No need to do anything, but cool to stop fatal errors.
|
||||
|
||||
} // end try;
|
||||
}
|
||||
|
||||
$object = apply_filters("wu_delete_form_get_object_{$model}", $object, $id, $model);
|
||||
|
||||
if (!$object) {
|
||||
|
||||
if ( ! $object) {
|
||||
wp_send_json_error(new \WP_Error('not-found', __('Object not found.', 'wp-ultimo')));
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle objects (default state)
|
||||
@ -493,26 +481,23 @@ class Form_Manager extends Base_Manager {
|
||||
$saved = $object->delete();
|
||||
|
||||
if (is_wp_error($saved)) {
|
||||
|
||||
wp_send_json_error($saved);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
do_action("wu_after_delete_{$model}_modal", $object);
|
||||
|
||||
$data_json_success = apply_filters("wu_data_json_success_delete_{$model}_modal", array(
|
||||
'redirect_url' => wu_network_admin_url("wp-ultimo-{$plural_name}", array('deleted' => 1))
|
||||
));
|
||||
$data_json_success = apply_filters(
|
||||
"wu_data_json_success_delete_{$model}_modal",
|
||||
array(
|
||||
'redirect_url' => wu_network_admin_url("wp-ultimo-{$plural_name}", array('deleted' => 1)),
|
||||
)
|
||||
);
|
||||
|
||||
wp_send_json_success($data_json_success);
|
||||
|
||||
} else {
|
||||
|
||||
wp_send_json_error(new \WP_Error('model-not-found', __('Something went wrong.', 'wp-ultimo')));
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end handle_model_delete_form;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the deletion confirmation form.
|
||||
@ -526,57 +511,64 @@ class Form_Manager extends Base_Manager {
|
||||
|
||||
$model = wu_request('model');
|
||||
|
||||
$fields = apply_filters("wu_bulk_actions_{$model}_{$action}", array(
|
||||
'confirm' => array(
|
||||
'type' => 'toggle',
|
||||
'title' => __('Confirm Action', 'wp-ultimo'),
|
||||
'desc' => __('Review this action carefully.', 'wp-ultimo'),
|
||||
'html_attr' => array(
|
||||
'v-model' => 'confirmed',
|
||||
$fields = apply_filters(
|
||||
"wu_bulk_actions_{$model}_{$action}",
|
||||
array(
|
||||
'confirm' => array(
|
||||
'type' => 'toggle',
|
||||
'title' => __('Confirm Action', 'wp-ultimo'),
|
||||
'desc' => __('Review this action carefully.', 'wp-ultimo'),
|
||||
'html_attr' => array(
|
||||
'v-model' => 'confirmed',
|
||||
),
|
||||
),
|
||||
),
|
||||
'submit_button' => array(
|
||||
'type' => 'submit',
|
||||
'title' => wu_slug_to_name($action),
|
||||
'placeholder' => wu_slug_to_name($action),
|
||||
'value' => 'save',
|
||||
'classes' => 'button button-primary wu-w-full',
|
||||
'wrapper_classes' => 'wu-items-end',
|
||||
'html_attr' => array(
|
||||
'v-bind:disabled' => '!confirmed',
|
||||
'submit_button' => array(
|
||||
'type' => 'submit',
|
||||
'title' => wu_slug_to_name($action),
|
||||
'placeholder' => wu_slug_to_name($action),
|
||||
'value' => 'save',
|
||||
'classes' => 'button button-primary wu-w-full',
|
||||
'wrapper_classes' => 'wu-items-end',
|
||||
'html_attr' => array(
|
||||
'v-bind:disabled' => '!confirmed',
|
||||
),
|
||||
),
|
||||
),
|
||||
'model' => array(
|
||||
'type' => 'hidden',
|
||||
'value' => $model,
|
||||
),
|
||||
'bulk_action' => array(
|
||||
'type' => 'hidden',
|
||||
'value' => wu_request('bulk_action'),
|
||||
),
|
||||
'ids' => array(
|
||||
'type' => 'hidden',
|
||||
'value' => implode(',', wu_request('bulk-delete', '')),
|
||||
),
|
||||
));
|
||||
'model' => array(
|
||||
'type' => 'hidden',
|
||||
'value' => $model,
|
||||
),
|
||||
'bulk_action' => array(
|
||||
'type' => 'hidden',
|
||||
'value' => wu_request('bulk_action'),
|
||||
),
|
||||
'ids' => array(
|
||||
'type' => 'hidden',
|
||||
'value' => implode(',', wu_request('bulk-delete', '')),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$form_attributes = apply_filters("wu_bulk_actions_{$action}_form", array(
|
||||
'views' => 'admin-pages/fields',
|
||||
'classes' => 'wu-modal-form wu-widget-list wu-striped wu-m-0 wu-mt-0',
|
||||
'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(
|
||||
'data-wu-app' => 'true',
|
||||
'data-state' => json_encode(array(
|
||||
'confirmed' => false,
|
||||
)),
|
||||
),
|
||||
));
|
||||
$form_attributes = apply_filters(
|
||||
"wu_bulk_actions_{$action}_form",
|
||||
array(
|
||||
'views' => 'admin-pages/fields',
|
||||
'classes' => 'wu-modal-form wu-widget-list wu-striped wu-m-0 wu-mt-0',
|
||||
'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(
|
||||
'data-wu-app' => 'true',
|
||||
'data-state' => json_encode(
|
||||
array(
|
||||
'confirmed' => false,
|
||||
)
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$form = new \WP_Ultimo\UI\Form('total-actions', $fields, $form_attributes);
|
||||
|
||||
$form->render();
|
||||
|
||||
} // end render_bulk_action_form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the deletion of customer.
|
||||
@ -597,8 +589,7 @@ class Form_Manager extends Base_Manager {
|
||||
do_action("wu_handle_bulk_action_form_{$model}_{$action}", $action, $model, $ids);
|
||||
|
||||
do_action('wu_handle_bulk_action_form', $action, $model, $ids);
|
||||
|
||||
} // end handle_bulk_action_form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default handler for bulk actions.
|
||||
@ -615,15 +606,13 @@ class Form_Manager extends Base_Manager {
|
||||
$status = \WP_Ultimo\List_Tables\Base_List_Table::process_bulk_action();
|
||||
|
||||
if (is_wp_error($status)) {
|
||||
|
||||
wp_send_json_error($status);
|
||||
}
|
||||
|
||||
} // end if;
|
||||
|
||||
wp_send_json_success(array(
|
||||
'redirect_url' => add_query_arg($action, count($ids), wu_get_current_url()),
|
||||
));
|
||||
|
||||
} // end default_bulk_action_handler;
|
||||
|
||||
} // end class Form_Manager;
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'redirect_url' => add_query_arg($action, count($ids), wu_get_current_url()),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user