Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Namespace
- namespace Leximo;
- /**
- * Metabox
- * @author Jan Doušek <mail@jandousek.cz>
- */
- class Metabox {
- /* @var string */
- const POSTS = 'posts';
- /* @var string */
- const TYPES = 'types';
- /* @var string */
- const TEMPLATES = 'templates';
- /* @var string */
- const IDS = 'ids';
- /* @var object */
- public static $template = null;
- /* @var string */
- private $id = null;
- /* @var string */
- private $title = null;
- /* @var string */
- private $prefix = null;
- /* @var string */
- private $position = 'content';
- /* @var array */
- private $limits = []; // [self::POSTS => [self::TYPES => [], self::TEMPLATES => [], self::IDS => []]]
- /* @var array */
- private $contents = [];
- /* @var object */
- private $form = null;
- /* @var object */
- private $object = null;
- /* @var array */
- private $errors = [];
- /* @var object */
- private $presenter = null;
- /**
- * Constructor
- * @param string $id
- * @param string $title
- * @param array $limits
- * @return void
- */
- public function __construct($id, $title, array $limits) {
- // Set id
- $this->id = LEXIMO_SLUG . '_metabox-' . $id;
- // Set title
- $this->title = $title;
- // Set limits
- $this->setLimits($limits);
- // Set presenter
- $this->presenter = Presenter::getInstance();
- // Set template
- self::$template = $this->presenter::$templatesPrivate . '/metabox';
- // Init new form
- $this->form = new Forms\Form($id);
- // Set form method
- $this->form->setMethod($this->form::POST);
- // Set object
- $this->setObject();
- // Set errors
- $this->setErrors();
- // Render draft message
- $this->renderDraftMessage();
- // Save
- $this->save();
- }
- /**
- * Set position
- * @param string $position
- * @return void
- */
- public function setPosition($position) {
- // Set position
- $this->position = $position;
- // Return class
- return $this;
- }
- /**
- * Set prefix
- * @param string $prefix
- * @return void
- */
- public function setPrefix($prefix) {
- // Set prefix
- $this->prefix = $prefix;
- // Return class
- return $this;
- }
- /**
- * Set limits
- * @param array $limits
- * @return void
- */
- private function setLimits($limits) {
- // Set limits
- $this->limits = $limits;
- // OLD version of set limits
- /* foreach($limits as $key=>$limit) {
- $this->limits[$key] = array_replace($this->limits[$key], $limit);
- } */
- }
- /**
- * Add title
- * @param string $id
- * @param string $title
- * @return void
- */
- public function addTitle($id, $title) {
- $this->contents[$id] = (object) ['type' => 'title', 'content' => $title];
- }
- /**
- * Add content
- * @param string $id
- * @param string $content
- * @return void
- */
- public function addContent($id, $content) {
- $this->contents[$id] = (object) ['type' => 'content', 'content' => $content];
- }
- /**
- * Add separator
- * @param int $height
- * @return void
- */
- public function addSeparator($height = 1) {
- $this->contents[] = (object) ['type' => 'separator', 'height' => intval($height)];
- }
- /**
- * Set object
- * @param int $id
- * @return void
- */
- private function setObject($id = null) {
- // Get globals
- global $pagenow;
- // Post
- if($pagenow == 'post.php' && $id = lex_value($id, $this->presenter->request->post)) {
- $this->object = new Posts\Single(['post_type' => $this->limits[self::POSTS][self::TYPES], 'post_status' => ['publish', 'draft', 'pending', 'private'], 'id' => $id]);
- }
- }
- /**
- * Set errors
- * @param void
- * @return void
- */
- private function setErrors() {
- $this->errors = !empty($this->object) && !$this->object->isEmpty() ? $this->object->meta->{$this->id . '_errors'} : [];
- }
- /**
- * Get errors
- * @param void
- * @return array
- */
- public function getErrors() {
- return !empty($this->errors) ? $this->errors : [];
- }
- /**
- * Get contents
- * @param string $id
- * @param string $content
- * @return void
- */
- public function getContents() {
- return $this->contents;
- }
- /**
- * Get form
- * @param void
- * @return object
- */
- public function getForm() {
- return $this->form;
- }
- /**
- * Save
- * @param void
- * @return void
- */
- private function save() {
- // Check post data
- if(!isset($_POST) || count($_POST) < 1) return false;
- // HOOK - save_post
- add_action('save_post', function() { global $post;
- // Clear form errors
- $this->form->clearErrors();
- // Form succeed
- $this->form->succeed();
- // Save post
- $this->savePost($post, $this->form->getErrors());
- });
- // HOOK - edit_term
- add_action('edit_term', function() {
- });
- }
- /**
- * Save post
- * @param object $id
- * @param array $errors
- * @return void
- */
- private function savePost($post, array $errors = []) {
- // Set globals
- global $pagenow;
- // Check pagenow
- if(!in_array($pagenow, ['post.php', 'edit.php'])) return;
- // Check post type and set object
- if(in_array($post->post_type, $this->limits[self::POSTS][self::TYPES])) {
- $this->setObject($post->ID);
- }
- // Check object
- if(!empty($this->object) && !$this->object->isEmpty()) {
- // Save meta informations
- foreach($this->form->getData(['form']) as $key=>$value) {
- if(isset($_POST) && isset($_POST[$key])) $this->object->meta->updateSingle($key, $value);
- }
- // Save errors
- $this->savePostErrors($this->object, $errors);
- // Change post status
- if(!empty($errors)) {
- $this->changePostStatus($this->object->id);
- }
- }
- }
- /**
- * Change post status
- * @param int $id
- * @param string $status
- * @return bool
- */
- private function changePostStatus($id, $status = 'draft') { global $wpdb;
- return $wpdb->update($wpdb->posts, ['post_status' => $status], ['id' => $id]);
- }
- /**
- * Save post errors
- * @param object $post
- * @param array $errors
- * @return bool
- */
- private function savePostErrors($post, array $errors = []) {
- // Set key
- $key = $this->id . '_errors';
- // Delete whole meta information
- if(empty($errors)) {
- return $post->meta->deleteSingle($key);
- }
- // Add or update meta information
- else {
- return $post->meta->updateSingle($key, $errors);
- }
- }
- /**
- * Register
- * @param void
- * @return void
- */
- public function register() {
- // Add metabox for post type
- if(!empty($this->limits['posts']['types']) && $this->isCorrectPostScreen()) {
- lex_print($this->id);
- // Create metabox for post
- $this->createMetaboxForPost();
- }
- // Add metabox for taxonomies
- if(!empty($this->limits['taxonomies']['types'])) {
- // Create metabox for taxonomies
- $this->createMetaboxForTaxonomies();
- }
- }
- /**
- * Is correct post screen
- * @param void
- * @return bool
- */
- private function isCorrectPostScreen() {
- // Check post types
- if(!empty($this->limits[self::POSTS][self::TYPES]) && empty($this->limits[self::POSTS][self::TEMPLATES]) && empty($this->limits[self::POSTS][self::IDS])) {
- lex_print('mrd');
- return true;
- }
- // Check object
- if(!empty($this->object) && !$this->object->isEmpty()) {
- // Check templates
- if(!empty($this->limits[self::POSTS][self::TEMPLATES]) && in_array($this->object->template, $this->limits[self::POSTS][self::TEMPLATES])) {
- return true;
- }
- // Check IDs
- if(!empty($this->limits[self::POSTS][self::IDS]) && in_array($this->object->id, $this->limits[self::POSTS][self::IDS])) {
- return true;
- }
- }
- }
- /**
- * Create metabox for post
- * @param void
- * @return void
- */
- private function createMetaboxForPost() {
- // Add action
- add_action('add_meta_boxes', function() {
- // Add metabox
- add_meta_box($this->id, $this->title, function() {
- // Add metabox to presenter
- $this->presenter->metabox = $this;
- // Render metabox
- $this->presenter->render(self::$template);
- },
- // Limit metabox to specific post types
- $this->limits[self::POSTS][self::TYPES]);
- });
- }
- /**
- * Create metabox for term
- * @param void
- * @return void
- */
- private function createMetaboxForTerm() {
- /* TODO */
- }
- /**
- * Render draft message
- * @param void
- * @return void
- */
- private function renderDraftMessage() {
- // Get globals
- global $pagenow;
- // Post
- if($pagenow == 'post.php' && !empty($this->errors) && $this->object->status == 'draft') {
- // HOOK action - edit_form_after_title
- add_action('edit_form_after_title', function() {
- // Render message
- $this->presenter->render($this->presenter::$templatesPrivate . '/flash', ['type' => 'danger', 'title' => LEXIMO_MESSAGES[301]]);
- });
- }
- }
- /**
- * Add field
- * @param string $method
- * @param array $args
- * @return mixed
- */
- private function addField($method, $args) {
- // Add prefix
- if($this->prefix) {
- $args[0] = $this->prefix . '_' . $args[0];
- }
- // Add field to form
- call_user_func_array([$this->form, $method], $args);
- // Get field
- $field = $this->form->getField($args[0])->setClasses(LEXIMO_SLUG . '-input');
- // Add field to contents
- $this->contents[$args[0]] = (object) ['type' => 'field'];
- // Check object
- if(!empty($this->object) && !$this->object->isEmpty()) {
- // Add value and set error
- $field->{($field->type == 'checkbox' ? 'setValue' : 'setDefault')}($this->object->meta->{$args[0]})
- ->setError(isset($this->errors[$args[0]]) ? $this->errors[$args[0]] : null);
- }
- // Return field
- return $field;
- }
- /**
- * __ Call
- * @param string $method
- * @param array $args
- * @return mixed
- */
- public function __call($method, $args) {
- // Add field
- if(substr($method, 0, 3) === 'add') {
- // Change file into media
- if($method == 'addFile') $method = 'addMedia';
- // Add field
- return $this->addField($method, $args);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement