Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/jet-engine   php

Repository URL to install this package:

Version: 2.7.7 

/ base / base-data.php

<?php
/**
 * Base data controller class
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

if ( ! class_exists( 'Jet_Engine_Base_Data' ) ) {

	/**
	 * Define Jet_Engine_Base_Data class
	 */
	abstract class Jet_Engine_Base_Data {

		/**
		 * DB manager instance
		 *
		 * @var Jet_Engine_DB
		 */
		public $db = null;

		/**
		 * Parent manager instance
		 *
		 * @var Jet_Engine_CPT
		 */
		public $parent = null;

		/**
		 * Table name
		 *
		 * @var string
		 */
		public $table = null;

		/**
		 * DB query arguments
		 *
		 * @var array
		 */
		public $query_args = array();

		/**
		 * Table format
		 *
		 * @var string
		 */
		public $table_format = array();

		/**
		 * Edit slug
		 *
		 * @var string
		 */
		public $edit = 'edit';

		public $raw = false;

		public $request = array();

		/**
		 * Constructir for the class
		 */
		function __construct( $parent ) {
			$this->db     = jet_engine()->db;
			$this->parent = $parent;
		}

		/**
		 * Set current requset data
		 *
		 * @param [type] $request [description]
		 */
		public function set_request( $request ) {
			$this->request = $request;
		}

		/**
		 * Create new post type
		 *
		 * @return void
		 */
		public function create_item( $redirect = true ) {

			if ( ! current_user_can( 'manage_options' ) ) {
				$this->parent->add_notice(
					'error',
					__( 'You don\'t have permissions to do this', 'jet-engine' )
				);
				return;
			}

			if ( ! $this->sanitize_item_request() ) {
				return;
			}

			$item = $this->sanitize_item_from_request();
			$id   = $this->update_item_in_db( $item );

			$this->after_item_update( $item, true );

			if ( ! $id ) {
				$this->parent->add_notice(
					'error',
					__( 'Couldn\'t create item', 'jet-engine' )
				);
				return;
			}

			flush_rewrite_rules();

			if ( method_exists( $this->parent, 'get_page_link' ) ) {
				$redirect_url = add_query_arg(
					array(
						'id'     => $id,
						'notice' => 'added',
					),
					$this->parent->get_page_link( $this->edit )
				);
			} else {
				$redirect_url = false;
			}

			if ( $redirect && $redirect_url ) {

				wp_redirect( $redirect_url );

				die();

			} else {
				return $id;
			}

		}

		/**
		 * Update current item in data base
		 *
		 * @param  [type] $item [description]
		 * @return [type]       [description]
		 */
		public function update_item_in_db( $item ) {

			if ( ! empty( $this->query_args ) ) {
				$item = array_merge( $item, $this->query_args );
			}

			return $this->db->update( $this->table, $item, $this->table_format );
		}

		/**
		 * Rewrite this function in the child class to perform any actions on item update
		 */
		public function before_item_update( $item ) {}

		/**
		 * Rewrite this function in the child class to perform any actions on item update
		 */
		public function after_item_update( $item, $is_new = false ) {}

		/**
		 * Rewrite this function in the child class to perform any actions on item delete
		 */
		public function before_item_delete( $item_id ) {}

		/**
		 * Update post post type
		 *
		 * @return void
		 */
		public function delete_item( $redirect = true ) {

			if ( ! current_user_can( 'manage_options' ) ) {

				$this->parent->add_notice(
					'error',
					__( 'You don\'t have permissions to do this', 'jet-engine' )
				);

				return;
			}

			$id = isset( $this->request['id'] ) ? esc_attr( $this->request['id'] ) : false;

			if ( ! $id ) {

				$this->parent->add_notice(
					'error',
					__( 'Please provide item ID to delete', 'jet-engine' )
				);

				return;

			}

			$this->before_item_delete( $id );

			$this->db->delete( $this->table, array( 'id' => $id ), array( '%d' ) );

			flush_rewrite_rules();

			if ( $redirect ) {
				if ( method_exists( $this->parent, 'get_page_link' ) ) {
					wp_redirect( $this->parent->get_page_link() );
					die();
				}
			} else {
				return true;
			}

		}

		/**
		 * Update post post type
		 *
		 * @return void
		 */
		public function edit_item( $redirect = true ) {

			if ( ! current_user_can( 'manage_options' ) ) {
				$this->parent->add_notice(
					'error',
					__( 'You don\'t have permissions to do this', 'jet-engine' )
				);
				return;
			}

			if ( ! $this->sanitize_item_request() ) {
				return;
			}

			$id = isset( $this->request['id'] ) ? esc_attr( $this->request['id'] ) : false;

			if ( ! $id ) {

				$this->parent->add_notice(
					'error',
					__( 'Item ID not passed', 'jet-engine' )
				);

				return;
			}

			$item       = $this->sanitize_item_from_request();
			$item['id'] = $id;

			$this->before_item_update( $item );

			$id = $this->update_item_in_db( $item );

			$this->after_item_update( $item );

			if ( ! $id ) {
				$this->parent->add_notice(
					'error',
					__( 'Couldn\'t update item', 'jet-engine' )
				);
				return;
			}

			flush_rewrite_rules();

			if ( $redirect ) {

				if ( method_exists( $this->parent, 'get_page_link' ) ) {

					wp_redirect( add_query_arg(
						array( 'id' => $id ),
						$this->parent->get_page_link( $this->edit )
					) );

					die();

				}

			} else {
				return true;
			}

		}

		/**
		 * Sanitizr post type request
		 *
		 * @return void
		 */
		public function sanitize_item_request() {

			$valid = true;

			if ( empty( $this->request['slug'] ) ) {
				$valid = false;
				$this->parent->add_notice(
					'error',
					__( 'Please set post type slug', 'jet-engine' )
				);
			}

			if ( empty( $this->request['name'] ) ) {
				$valid = false;
				$this->parent->add_notice(
					'error',
					__( 'Please set post type name', 'jet-engine' )
				);
			}

			if ( isset( $this->request['slug'] ) && in_array( $this->request['slug'], $this->items_blacklist() ) ) {
				$valid = false;
				$this->parent->add_notice(
					'error',
					__( 'Please change post type slug. Current is reserved for WordPress needs', 'jet-engine' )
				);
			}

			/**
			 * @todo  fix validation
			 */

			return $valid;

		}

		/**
		 * Sanizitze slug
		 *
		 * @param  [type] $slug [description]
		 * @return [type]       [description]
		 */
		public function sanitize_slug( $slug ) {

			$slug = esc_attr( $slug );
			$slug = strtolower( $slug );
			$slug = remove_accents( $slug );
			$slug = preg_replace( '/[^a-z0-9\s\-\_]/', '', $slug );
			$slug = str_replace( ' ', '-', $slug );

			return $slug;

		}

		/**
		 * Ensure that required database table is exists, create if not.
Loading ...