*/ protected $trigger_labels = array(); /** * Constructor. * * @param string $base_url Base URL for links. */ public function __construct( $base_url = '' ) { $this->base_url = $base_url ?: HubPage::url( array( 'tab' => 'automations' ) ); $this->trigger_labels = $this->getTriggerLabels(); parent::__construct( array( 'singular' => 'automation', 'plural' => 'automations', 'ajax' => false, ) ); } /** * Column definitions. * * @return array */ public function get_columns() { return array( 'name' => _x( 'Name', 'column name', 'afroit-messaging-workflows' ), 'trigger' => _x( 'Trigger', 'column name', 'afroit-messaging-workflows' ), 'is_active' => _x( 'Active', 'column name', 'afroit-messaging-workflows' ), 'actions' => _x( 'Actions', 'column name', 'afroit-messaging-workflows' ), ); } /** * Primary column. * * @return string */ protected function get_primary_column_name() { return 'name'; } /** * Override to ensure columns are set. * * @return array */ protected function get_column_info() { if ( isset( $this->_column_headers ) && is_array( $this->_column_headers ) && 4 === count( $this->_column_headers ) ) { return $this->_column_headers; } $columns = $this->get_columns(); $hidden = $this->screen ? get_hidden_columns( $this->screen ) : array(); $sortable = array(); $primary = $this->get_primary_column_name(); $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); return $this->_column_headers; } /** * Prepare items with data. * * @param array $items Workflow items for current page. * @param int $total_items Total number of workflows. * @param int $per_page Items per page. Default 10. * @return void */ public function prepare_items_with_data( $items, $total_items = null, $per_page = 10 ) { $this->items = is_array( $items ) ? $items : array(); $total = null !== $total_items ? (int) $total_items : count( $this->items ); $per_page = (int) $per_page; $total_pages = $per_page > 0 ? (int) ceil( $total / $per_page ) : 1; $this->set_pagination_args( array( 'total_items' => $total, 'per_page' => $per_page, 'total_pages' => $total_pages, ) ); } /** * Required by parent. */ public function prepare_items() { $this->items = array(); } /** * Message when no items. */ public function no_items() { echo esc_html__( 'No automations found.', 'afroit-messaging-workflows' ); } /** * Column: name. * * @param Workflow $item Workflow. * @return string */ protected function column_name( $item ) { return '' . esc_html( $item->getName() ) . ''; } /** * Column: trigger. * * @param Workflow $item Workflow. * @return string */ protected function column_trigger( $item ) { $trigger = $item->getTrigger(); $label = isset( $this->trigger_labels[ $trigger ] ) ? $this->trigger_labels[ $trigger ] : $trigger; return esc_html( $label ); } /** * Column: is_active. * * @param Workflow $item Workflow. * @return string */ protected function column_is_active( $item ) { $active = $item->isActive(); return $active ? '' . esc_html__( 'Yes', 'afroit-messaging-workflows' ) . '' : '' . esc_html__( 'No', 'afroit-messaging-workflows' ) . ''; } /** * Column: actions (Edit, Delete). * * @param Workflow $item Workflow. * @return string */ protected function column_actions( $item ) { $id = $item->getId(); if ( ! $id ) { return ''; } $edit_url = HubPage::url( array( 'tab' => 'create_automation', 'workflow_id' => $id, ) ); $toggle_url = add_query_arg( array( 'action' => 'afromewo_toggle_automation', 'workflow_id' => $id, '_wpnonce' => wp_create_nonce( 'toggle_automation_' . $id ), ), admin_url( 'admin-post.php' ) ); $delete_url = add_query_arg( array( 'action' => 'afromewo_delete_automation', 'workflow_id' => $id, '_wpnonce' => wp_create_nonce( 'delete_automation_' . $id ), ), admin_url( 'admin-post.php' ) ); $active = $item->isActive(); $toggle_label = $active ? __( 'Deactivate', 'afroit-messaging-workflows' ) : __( 'Activate', 'afroit-messaging-workflows' ); $toggle_class = $active ? 'button button-small' : 'button button-small button-primary'; $out = '' . esc_html__( 'Edit', 'afroit-messaging-workflows' ) . ' '; $out .= '' . esc_html( $toggle_label ) . ' '; $out .= '' . esc_html__( 'Delete', 'afroit-messaging-workflows' ) . ''; return $out; } /** * Default column. * * @param Workflow $item Workflow. * @param string $column_name Column key. * @return string */ protected function column_default( $item, $column_name ) { return ''; } /** * Hide bulk actions. * * @return array */ protected function get_bulk_actions() { return array(); } /** * Hide bulk actions dropdown. * * @param string $which 'top' or 'bottom'. */ protected function bulk_actions( $which = '' ) { // No bulk actions. } protected function getTriggerLabels() { $labels = array(); $triggers = WorkflowTriggers::getTriggers(); foreach ($triggers as $trigger => $data) { $labels[$trigger] = $data['label'] ?? $trigger; } asort($labels); return $labels; } }