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 

/ components / options-pages / manager.php

<?php
/**
 * Options pages manager
 */

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

if ( ! class_exists( 'Jet_Engine_Options_Pages' ) ) {

	/**
	 * Define Jet_Engine_Options_Pages class
	 */
	class Jet_Engine_Options_Pages extends Jet_Engine_Base_WP_Intance {

		/**
		 * Base slug for CPT-related pages
		 * @var string
		 */
		public $page = 'jet-engine-options-pages';

		/**
		 * Action request key
		 *
		 * @var string
		 */
		public $action_key = 'cpt_action';

		/**
		 * Metaboxes to register
		 *
		 * @var array
		 */
		public $meta_boxes = array();

		/**
		 * Set object type
		 * @var string
		 */
		public $object_type = 'options';

		/**
		 * All options pages objects
		 *
		 * @var array
		 */
		public $registered_pages = array();

		/**
		 * Options list to use as select options
		 *
		 * @var array
		 */
		public $options_list = array();

		/**
		 * Option fields with `save custom` option
		 *
		 * @var array
		 */
		public $option_fields_save_custom = array();

		/**
		 * Init data instance
		 *
		 * @return [type] [description]
		 */
		public function init_data() {

			if ( ! class_exists( 'Jet_Engine_Base_Data' ) ) {
				require_once jet_engine()->plugin_path( 'includes/base/base-data.php' );
			}

			require $this->component_path( 'data.php' );

			$this->data = new Jet_Engine_Options_Data( $this );

		}

		/**
		 * Initiizlize post type specific API endpoints
		 *
		 * @param  Jet_Engine_REST_API $api_manager API manager instance.
		 * @return void
		 */
		public function init_rest( $api_manager ) {

			require_once $this->component_path( 'rest-api/add-options-page.php' );
			require_once $this->component_path( 'rest-api/edit-options-page.php' );
			require_once $this->component_path( 'rest-api/get-options-page.php' );
			require_once $this->component_path( 'rest-api/delete-options-page.php' );
			require_once $this->component_path( 'rest-api/get-options-pages.php' );

			$api_manager->register_endpoint( new Jet_Engine_Rest_Add_Options_Page() );
			$api_manager->register_endpoint( new Jet_Engine_Rest_Edit_Options_Page() );
			$api_manager->register_endpoint( new Jet_Engine_Rest_Get_Options_Page() );
			$api_manager->register_endpoint( new Jet_Engine_Rest_Delete_Options_Page() );
			$api_manager->register_endpoint( new Jet_Engine_Rest_Get_Options_Pages() );

		}

		/**
		 * Return path to file inside component
		 *
		 * @param  [type] $path_inside_component [description]
		 * @return [type]                        [description]
		 */
		public function component_path( $path_inside_component ) {
			return jet_engine()->plugin_path( 'includes/components/options-pages/' . $path_inside_component );
		}

		/**
		 * Register new options page by passed arguments
		 *
		 * @return [type] [description]
		 */
		public function register_new_options_page( $args = array() ) {
			$page                                  = new Jet_Engine_Options_Page_Factory( $args );
			$this->registered_pages[ $page->slug ] = $page;
			$this->options_list[]                  = $page->get_options_for_select();
		}

		/**
		 * Register created post types
		 *
		 * @return void
		 */
		public function register_instances() {

			require_once $this->component_path( 'options-page.php' );

			$children = array();

			foreach ( $this->get_items() as $item ) {

				if ( empty( $item['parent'] ) ) {
					$this->register_new_options_page( $item );
				} else {
					$children[] = $item;
				}

				$this->find_option_fields_with_save_custom( $item['slug'], $item['fields'], $item['capability'] );

			}

			if ( ! empty( $children ) ) {
				foreach ( $children as $item ) {
					$this->register_new_options_page( $item );
				}
			}

			$this->add_hooks_to_save_custom_values();

		}

		/**
		 * Find option fields with enabling `save custom` option
		 *
		 * @param $page
		 * @param $fields
		 * @param $capability
		 */
		public function find_option_fields_with_save_custom( $page, $fields, $capability ) {
			foreach ( $fields as $field ) {

				if ( 'field' !== $field['object_type'] || ! in_array( $field['type'], array( 'checkbox', 'radio' ) ) ) {
					continue;
				}

				$allow_custom = ! empty( $field['allow_custom'] ) && filter_var( $field['allow_custom'], FILTER_VALIDATE_BOOLEAN );
				$save_custom  = ! empty( $field['save_custom'] ) && filter_var( $field['save_custom'], FILTER_VALIDATE_BOOLEAN );

				if ( ! $allow_custom || ! $save_custom ) {
					continue;
				}

				if ( empty( $this->option_fields_save_custom[ $page ] ) ) {
					$this->option_fields_save_custom[ $page ] = array(
						'capability' => ! empty( $capability ) ? $capability : 'manage_options',
						'fields'     => array(),
					);
				}

				$this->option_fields_save_custom[ $page ]['fields'][ $field['name'] ] = $field;
			}
		}

		/**
		 * Add hooks to save custom values
		 */
		public function add_hooks_to_save_custom_values() {

			if ( empty( $this->option_fields_save_custom ) ) {
				return;
			}

			add_action( 'admin_init', array( $this, 'save_custom_values' ) );
		}

		/**
		 * Save custom values
		 */
		public function save_custom_values() {

			if ( ! isset( $_REQUEST['action'] ) || 'jet-engine-op-save-settings' !== $_REQUEST['action']
			     || ! isset( $_REQUEST['page'] ) || ! in_array( $_REQUEST['page'], array_keys( $this->option_fields_save_custom ) )
			) {
				return;
			}

			$page       = $_REQUEST['page'];
			$capability = $this->option_fields_save_custom[ $page ]['capability'];

			if ( ! current_user_can( $capability ) ) {
				return;
			}

			$query_args = array_merge( $this->data->query_args, array( 'slug' => $page ) );

			$item = $this->data->db->query(
				$this->data->table,
				$query_args
			);

			if ( empty( $item ) ) {
				return;
			}

			$item        = $item[0];
			$meta_fields = maybe_unserialize( $item['meta_fields'] );
			$update      = false;

			foreach ( $this->option_fields_save_custom[ $page ]['fields'] as $field => $field_args ) {

				if ( ! isset( $_POST[ $field ] ) || '' === $_POST[ $field ] ) {
					continue;
				}

				do_action( 'jet-engine/meta-boxes/save-custom-value', $field, $field_args );

				$_meta_fields = jet_engine()->meta_boxes->maybe_add_custom_values_to_options( $meta_fields, $field, $field_args );

				if ( $_meta_fields ) {
					$meta_fields = $_meta_fields;
					$update      = true;
				}
			}

			if ( $update ) {
				$item['meta_fields'] = maybe_serialize( $meta_fields );
				$this->data->update_item_in_db( $item );
			}
		}

		/**
		 * Returns all registered options (or depends on context) to use in select
		 *
		 * @return [type] [description]
		 */
		public function get_options_for_select( $context = 'plain', $where = 'elementor' ) {

			$result = array();

			foreach ( $this->options_list as $slug => $data ) {

				$group        = array();
				$blocks_group = array();

				foreach ( $data['options'] as $name => $field_data ) {

					switch ( $context ) {

						case 'plain':

							$black_list = array( 'repeater', 'html', 'tab', 'accordion', 'endpoint' );

							if ( ! in_array( $field_data['type'], $black_list ) ) {
								$group[ $name ] = $field_data['title'];

								$blocks_group[] = array(
									'value' => $name,
									'label' => $field_data['title'],
								);
							}

							break;

						case 'repeater':

							if ( 'repeater' === $field_data['type'] ) {
								$group[ $name ] = $field_data['title'];

								$blocks_group[] = array(
									'value' => $name,
									'label' => $field_data['title'],
								);

							}

							break;

						case 'media':

							if ( 'media' === $field_data['type'] ) {
								$group[ $name ] = $field_data['title'];

								$blocks_group[] = array(
									'value' => $name,
									'label' => $field_data['title'],
								);

							}

							break;

						case 'gallery':

							if ( 'gallery' === $field_data['type'] ) {
								$group[ $name ] = $field_data['title'];

								$blocks_group[] = array(
									'value' => $name,
									'label' => $field_data['title'],
								);

							}

							break;

						case 'all':

							$group[ $name ] = $field_data['title'];

							$blocks_group[] = array(
								'value' => $name,
								'label' => $field_data['title'],
							);

							break;

					}

				}
Loading ...