errors === null) {
$this->errors = new \WP_Error;
} // end if;
return $this->errors;
} // end get_errors;
/**
* Register additional hooks to page load such as the action links and the save processing.
*
* @since 2.0.0
* @return void
*/
public function page_loaded() {
/**
* Setups the object
*/
$this->object = $this->get_object();
$this->edit = $this->object->exists();
/**
* Deals with lock statuses.
*/
$this->add_lock_notices();
if (wu_request('submit_button') === 'delete') {
$this->process_delete();
} elseif (wu_request('remove-lock')) {
$this->remove_lock();
} else {
/*
* Process save, if necessary
*/
$this->process_save();
} // end if;
} // end page_loaded;
/**
* Add some other necessary hooks.
*
* @return void
*/
public function hooks() {
parent::hooks();
add_filter('removable_query_args', array($this, 'removable_query_args'));
} // end hooks;
/**
* Adds the wu-new-model to the list of removable query args of WordPress.
*
* @since 2.0.0
*
* @param array $removable_query_args Existing list of removable query args.
* @return array
*/
public function removable_query_args($removable_query_args) {
$removable_query_args[] = 'wu-new-model';
return $removable_query_args;
} // end removable_query_args;
/**
* Displays lock notices, if necessary.
*
* @since 2.0.0
* @return void
*/
protected function add_lock_notices() {
$locked = $this->get_object()->is_locked();
if ($locked && $this->edit) {
// translators: %s is the date, using the site format options
$message = sprintf(__('This item is locked from editions.
This is probably due to a background action being performed (like a transfer between different accounts, for example). You can manually unlock it, but be careful. The lock should be released automatically in %s seconds.', 'wp-ultimo'), wu_get_next_queue_run() + 10);
$actions = array(
'preview' => array(
'title' => __('Unlock', 'wp-ultimo'),
'url' => add_query_arg(array(
'remove-lock' => 1,
'unlock_wpultimo_nonce' => wp_create_nonce(sprintf('unlocking_%s', $this->object_id)),
)),
),
);
WP_Ultimo()->notices->add($message, 'warning', 'network-admin', false, $actions);
} // end if;
} // end add_lock_notices;
/**
* Remove the lock from the object.
*
* @since 2.0.0
* @return void
*/
public function remove_lock() {
$unlock_tag = "unlocking_{$this->object_id}";
if (isset($_REQUEST['remove-lock'])) {
check_admin_referer($unlock_tag, 'unlock_wpultimo_nonce');
/**
* Allow plugin developers to add actions to the unlocking process.
*
* @since 1.8.2
*/
do_action("wu_unlock_{$this->object_id}");
/**
* Unlocks and redirects.
*/
$this->get_object()->unlock();
wp_redirect(remove_query_arg(array(
'remove-lock',
'unlock_wpultimo_nonce',
)));
exit;
} // end if;
} // end remove_lock;
/**
* Handles saves, after verifying nonces and such. Should not be rewritten by child classes.
*
* @since 2.0.0
* @return void
*/
final public function process_save() {
$saving_tag = "saving_{$this->object_id}";
if (isset($_REQUEST[$saving_tag])) {
check_admin_referer($saving_tag, '_wpultimo_nonce');
/**
* Allow plugin developers to add actions to the saving process
*
* @since 1.8.2
*/
do_action("wu_save_{$this->object_id}", $this);
/**
* Calls the saving function
*/
$status = $this->handle_save();
if ($status) {
exit;
} // end if;
} // end if;
} // end process_save;
/**
* Handles delete, after verifying nonces and such. Should not be rewritten by child classes.
*
* @since 2.0.0
* @return void
*/
final public function process_delete() {
$deleting_tag = "deleting_{$this->object_id}";
if (isset($_REQUEST[$deleting_tag])) {
check_admin_referer($deleting_tag, 'delete_wpultimo_nonce');
/**
* Allow plugin developers to add actions to the deleting process
*
* @since 1.8.2
*/
do_action("wu_delete_{$this->object_id}");
/**
* Calls the deleting function
*/
$this->handle_delete();
} // end if;
} // end process_delete;
/**
* Returns the labels to be used on the admin page.
*
* @since 2.0.0
* @return array
*/
public function get_labels() {
$default_labels = array(
'edit_label' => __('Edit Object', 'wp-ultimo'),
'add_new_label' => __('Add New Object', 'wp-ultimo'),
'updated_message' => __('Object updated with success!', 'wp-ultimo'),
'title_placeholder' => __('Enter Object Name', 'wp-ultimo'),
'title_description' => '',
'save_button_label' => __('Save', 'wp-ultimo'),
'save_description' => '',
'delete_button_label' => __('Delete', 'wp-ultimo'),
'delete_description' => __('Be careful. This action is irreversible.', 'wp-ultimo'),
);
return apply_filters('wu_edit_admin_page_labels', $default_labels);
} // end get_labels;
/**
* Allow child classes to register scripts and styles that can be loaded on the output function, for example.
*
* @since 1.8.2
* @return void
*/
public function register_scripts() {
parent::register_scripts();
/*
* Enqueue the base Dashboard Scripts
*/
wp_enqueue_script('dashboard');
/*
* Adds Vue.
*/
wp_enqueue_script('wu-vue-apps');
wp_enqueue_script('wu-fields');
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('wu-selectizer');
} // end register_scripts;
/**
* Registers widgets to the edit page.
*
* This implementation register the default save widget.
* Child classes that wish to inherit that widget while registering other,
* can do such by adding a parent::register_widgets() to their own register_widgets() method.
*
* @since 2.0.0
* @return void
*/
public function register_widgets() {
$screen = get_current_screen();
$this->add_info_widget('info', array(
'title' => __('Timestamps', 'wp-ultimo'),
'position' => 'side-bottom',
));
if ($this->edit) {
$this->add_delete_widget('delete', array());
} // end if;
} // end register_widgets;
/**
* Adds a basic widget with info (and fields) to be shown.
*
* @since 2.0.0
*
* @param string $id Unique ID for the widget, since we can have more than one per page.
* @param array $atts Array containing the attributes to be passed to the widget.
* @return void
*/
protected function add_info_widget($id, $atts = array()) {
$created_key = 'date_created';
if (method_exists($this->get_object(), 'get_date_registered')) {
$created_key = 'date_registered';
} // end if;
$created_value = call_user_func(array($this->get_object(), "get_$created_key"));
$atts['fields'][$created_key] = array(
'title' => __('Created at', 'wp-ultimo'),
'type' => 'text-display',
'date' => true,
'display_value' => $this->edit ? $created_value : false,
'value' => $created_value,
'placeholder' => '2020-04-04 12:00:00',
'html_attr' => array(
'wu-datepicker' => 'true',
'data-format' => 'Y-m-d H:i:S',
'data-allow-time' => 'true',
),
);
$show_modified = wu_get_isset($atts, 'modified', true);
if ($this->edit && $show_modified === true) {
$atts['fields']['date_modified'] = array(
'title' => __('Last Modified at', 'wp-ultimo'),
'type' => 'text-display',
'date' => true,
'display_value' => $this->edit ? $this->get_object()->get_date_modified() : __('No date', 'wp-ultimo'),
'value' => $this->get_object()->get_date_modified(),
'placeholder' => '2020-04-04 12:00:00',
'html_attr' => array(
'wu-datepicker' => 'true',
'data-format' => 'Y-m-d H:i:S',
'data-allow-time' => 'true',
),
);
} // end if;
$this->add_fields_widget($id, $atts);
} // end add_info_widget;
/**
* Adds a basic widget to display list tables.
*
* @since 2.0.0
*
* @param string $id Unique ID for the widget, since we can have more than one per page.
* @param array $atts Array containing the attributes to be passed to the widget.
* @return void
*/
protected function add_list_table_widget($id, $atts = array()) {
$atts = wp_parse_args($atts, array(
'widget_id' => $id,
'before' => '',
'after' => '',
'title' => __('List Table', 'wp-ultimo'),
'position' => 'advanced',
'screen' => get_current_screen(),
'page' => $this,
'labels' => $this->get_labels(),
'object' => $this->get_object(),
'edit' => true,
'table' => false,
'query_filter' => false,
));
$atts['table']->set_context('widget');
$table_name = $atts['table']->get_table_id();
if (is_callable($atts['query_filter'])) {
add_filter("wu_{$table_name}_get_items", $atts['query_filter']);
} // end if;
add_filter('wu_events_list_table_get_columns', function($columns) {
unset($columns['object_type']);
unset($columns['code']);
return $columns;
});
add_meta_box("wp-ultimo-list-table-{$id}", $atts['title'], function() use ($atts) {
wp_enqueue_script('wu-ajax-list-table');
wu_get_template('base/edit/widget-list-table', $atts);
}, $atts['screen']->id, $atts['position'], 'default');
} // end add_list_table_widget;
/**
* Adds field widgets to edit pages with the same Form/Field APIs used elsewhere.
*
* @see Take a look at /inc/ui/form and inc/ui/field for reference.
* @since 2.0.0
*
* @param string $id ID of the widget.
* @param array $atts Array of attributes to pass to the form.
* @return void
*/
protected function add_fields_widget($id, $atts = array()) {
$atts = wp_parse_args($atts, array(
'widget_id' => $id,
'before' => '',
'after' => '',
'title' => __('Fields', 'wp-ultimo'),
'position' => 'side',
'screen' => get_current_screen(),
'fields' => array(),
'html_attr' => array(),
'classes' => '',
'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',
));
add_meta_box("wp-ultimo-{$id}-widget", $atts['title'], function() use ($atts) {
if (wu_get_isset($atts['html_attr'], 'data-wu-app')) {
$atts['fields']['loading'] = array(
'type' => 'note',
'desc' => sprintf('