示例#1
0
 /**
  * Create post type table column
  *
  * @param PostType|string $post_type
  * @param string $title
  * @param callable $function
  * @param bool $sortable
  * @param int $position
  */
 public function __construct($post_type, $title, $function, $sortable = false, $position = -1)
 {
     if (is_array($title)) {
         $this->_title = $title[1];
         $this->_key = sanitize_key($title[0]);
     } else {
         $this->_title = $title;
         $this->_key = sanitize_key($title);
     }
     $this->_position = $position;
     $this->_sortable = $sortable;
     $this->_function = $function;
     if ($post_type instanceof PostType) {
         $post_type = $post_type->get_key();
     }
     add_action("manage_edit-{$post_type}_columns", function ($columns) {
         return $this->_add_column($columns, [$this->_key => $this->_title], $this->_position);
     });
     add_action("manage_{$post_type}_posts_custom_column", function ($column) {
         if ($column == $this->_key) {
             return Action::execute($this->_function, $column);
         }
         return null;
     });
     if ($sortable) {
         add_filter("manage_edit-{$post_type}_sortable_columns", function ($columns) {
             return array_merge($columns, [$this->_key => $this->_key]);
         });
     }
 }
示例#2
0
 /**
  * Create transient cache with background updates
  *
  * @param string $key unique transient key
  * @param callable $callback update function
  * @param string $recurrence how often the cache should be updated, default is 'hourly'
  *
  * @throws WpException
  */
 public static function create($key, $callback, $recurrence = 'hourly')
 {
     $key = sanitize_key($key);
     if (empty($key)) {
         throw new WpException('Invalid Transient key');
     }
     if (!Action::is_callable($callback)) {
         throw new WpException('Invalid Transient callback');
     }
     $schedules = wp_get_schedules();
     if (!isset($schedules[$recurrence])) {
         throw new WpException('Invalid Transient recurrence');
     }
     $cron_action_key = static::_get_cron_action_key($key);
     add_action($cron_action_key, function () use($key, $callback) {
         $value = Action::execute($callback);
         set_transient($key, $value, 0);
     });
     if (!wp_next_scheduled($cron_action_key)) {
         wp_schedule_event(time(), $recurrence, $cron_action_key);
     }
 }
示例#3
0
 /**
  * Get taxonomy field
  *
  * @return AbstractField
  * @throws WpException
  */
 public function get_field()
 {
     if ($this->_field == null) {
         if ($this->_field_init == null) {
             $this->_field = new Text();
         } elseif (is_string($this->_field_init)) {
             $this->_field = FieldFactory::build($this->_field_init);
         } elseif (Action::is_callable($this->_field_init)) {
             $this->_field = Action::execute($this->_field_init);
             if (!$this->_field instanceof AbstractField) {
                 throw new WpException("Option \"{$this->_title}\" init function must return a Field.");
             }
         } else {
             throw new WpException("Invalid field type.");
         }
         $this->_field->set_name($this->_key);
         $this->_field->set_label($this->_title);
     }
     return $this->_field;
 }
示例#4
0
 protected function _get_fields()
 {
     if ($this->_fields == null || count($this->_fields) < count($this->_fields_init)) {
         foreach ($this->_fields_init as $_key => $field_init) {
             if (array_key_exists($_key, $this->_fields)) {
                 continue;
             }
             list($key, $title, $field) = $field_init;
             if ($field == null) {
                 $this->_fields[$key] = new Text();
             } elseif (is_string($field)) {
                 $this->_fields[$key] = FieldFactory::build($field);
             } elseif (Action::is_callable($field)) {
                 $this->_fields[$key] = Action::execute($field);
                 if (!$this->_fields[$key] instanceof AbstractField) {
                     throw new WpException("Meta box field \"{$title}\" init function must return a Field.");
                 }
             } else {
                 throw new WpException("Invalid field type.");
             }
             $this->_fields[$key]->set_name($key);
             $this->_fields[$key]->set_label($title);
         }
     }
     return $this->_fields;
 }
示例#5
0
文件: Table.php 项目: redink-no/wpkit
 /**
  * Execute table items actions
  */
 public function process_action()
 {
     $action = $this->_get_current_action();
     $this->_setup_view($action);
     if ($action && array_key_exists($action, $this->_actions)) {
         $function = $this->_actions[$action]->function;
         $args = ['action' => $action, 'item' => $_REQUEST['item']];
         Action::execute($function, $args);
         wp_redirect(remove_query_arg(['action', 'action2', 'item'], wp_unslash($_SERVER['REQUEST_URI'])));
         exit;
     }
 }
示例#6
0
 /**
  * @param $key
  * @param $title
  * @param null $field
  * @return AbstractField
  * @throws \WPKit\Exception\WpException
  */
 protected function _add_field($key, $title, $field = null)
 {
     $key = sanitize_key($key);
     if ($field == null) {
         $this->_fields[$key] = new Text();
     } elseif (is_string($field)) {
         $this->_fields[$key] = FieldFactory::build($field);
     } elseif (Action::is_callable($field)) {
         $this->_fields[$key] = Action::execute($field);
         if (!$this->_fields[$key] instanceof AbstractField) {
             throw new WpException("Widget field \"{$title}\" init function must return a Field.");
         }
     } else {
         throw new WpException("Invalid field type.");
     }
     $this->_fields[$key]->set_id($this->get_field_id($key));
     $this->_fields[$key]->set_name($this->get_field_name($key), false);
     $this->_fields[$key]->set_label($title);
     return $this->_fields[$key];
 }
示例#7
0
 /**
  * Ger column html
  *
  * @param array $item
  * @param string $key
  * @throws \WPKit\Exception\WpException
  */
 public function render($item, $key)
 {
     return Action::execute($this->_function, [$item, $key]);
 }