/** * 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); } }
/** * 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; }
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; }
/** * @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]; }