loadOptionData(); $this->id = is_array( $d ) && isset( $d['id'] ) ? $d['id'] : $d; if ( is_array( $d ) ) $this->data = stripslashes_deep( $d ); foreach ( $this->vars as $var ) { $this->$var = $this->get( $var ); } } static function findAll() { self::loadOptionData(); $records = array(); foreach ( self::$option_data as $id => $record ) { $record['id'] = $id; $model = new Vc_Automap_Model( $record ); if ( $model ) $records[] = $model; } return $records; } final protected static function loadOptionData() { if ( is_null( self::$option_data ) ) self::$option_data = Configuration::get( self::$option_name ); if ( ! self::$option_data ) self::$option_data = array(); return self::$option_data; } function get( $key ) { if ( is_null( $this->data ) ) $this->data = isset( self::$option_data[$this->id] ) ? self::$option_data[$this->id] : array(); return isset( $this->data[$key] ) ? $this->data[$key] : null; } function set( $attr, $value = null ) { if ( is_array( $attr ) ) { foreach ( $attr as $key => $value ) { $this->set( $key, $value ); } } elseif ( ! is_null( $value ) ) { $this->$attr = $value; } } function save() { if ( ! $this->isValid() ) return false; foreach ( $this->vars as $var ) { $this->data[$var] = $this->$var; } return $this->saveOption(); } function delete() { return $this->deleteOption(); } public function isValid() { if ( ! is_string( $this->name ) || empty( $this->name ) ) { return false; } if ( ! preg_match( '/^\S+$/', $this->tag ) ) return false; return true; } protected function saveOption() { self::$option_data[$this->id] = $this->data; return Configuration::updateValue( self::$option_name, self::$option_data ); } protected function deleteOption() { unset( self::$option_data[$this->id] ); return Configuration::updateValue( self::$option_name, self::$option_data ); } } } if ( ! class_exists( 'Vc_Automapper' ) ) { /** * Automated shortcode mapping * * Automapper adds settings tab for VC settings tabs with ability to map custom shortcodes to VC editors, * if shortcode is not mapped by default or developers haven't done this yet. * No more shortcode copy/paste. Add any third party shortcode to the list of VC menu elements for reuse. * Edit params, values and description. * * @since 4.1 */ class Vc_Automapper { protected static $disabled = false; public function __construct() { $jscomposer = JsComposer::getInstance(); $this->title = $jscomposer->l('My shortcodes'); } public function addAjaxActions() { // add_action( 'wp_ajax_vc_automapper', array( &$this, 'goAction' ) ); $actions = array( 'vc_automapper' => array( &$this, 'goAction' ) ); if($action = Tools::getValue('action')){ if(isset($actions[$action])) call_user_func ($actions[$action]); } } public function build() { $jscomposer = JsComposer::getInstance(); wp_register_script( 'wpb_js_composer_automapper', vc_asset_url( 'js/backend/composer-automapper.js' ), array( 'wpb_js_composer_settings', 'backbone', 'shortcode' ), WPB_VC_VERSION, true ); // TODO: remove to automapper render wp_enqueue_script( 'wpb_js_composer_automapper' ); wp_localize_script( 'wpb_js_composer_automapper', 'i18nLocaleVcAutomapper', array( 'are_you_sure_delete' => $jscomposer->l('Are you sure you want to delete this shortcode?'), 'are_you_sure_delete_param' => __( "Are you sure you want to delete the shortcode's param?", 'js_composer' ), 'my_shortcodes_category' => $jscomposer->l('My shortcodes'), 'error_shortcode_name_is_required' => __( "Shortcode name is required.", 'js_composer' ), 'error_enter_valid_shortcode_tag' => __( "Please enter valid shortcode tag.", 'js_composer' ), 'error_enter_required_fields' => __( "Please enter all required fields for params.", 'js_composer' ), 'new_shortcode_mapped' => $jscomposer->l('New shortcode mapped from string!'), 'shortcode_updated' => $jscomposer->l('Shortcode updated!'), 'error_content_param_not_manually' => $jscomposer->l('Content param can not be added manually, please use checkbox.'), 'error_param_already_exists' => $jscomposer->l('Param %s already exists. Param names must be unique.'), 'error_wrong_param_name' => $jscomposer->l('Please use only letters, numbers and underscore for param name'), 'error_enter_valid_shortcode' => $jscomposer->l('Please enter valid shortcode to parse!') ) ); } // Render methods {{ /** * Builds html for Automapper CRUD like administration block * * @return bool */ public function renderHtml() { if ( $this->disabled() ) return false; ?>

renderTemplates() ?> ' . '' . '' . '' . '' . ''; } public function renderMapFormTpl() { ?> renderMapFormTpl(); } // }} // Action methods(CRUD) {{ public function goAction() { $jscomposer = JsComposer::getInstance(); $action = $jscomposer->vc_post_param( 'vc_action' ); $this->result( $this->$action() ); } public function create() { $data = vc_post_param( 'data' ); $shortcode = new Vc_Automap_Model( $data ); return $shortcode->save(); } public function update() { $id = vc_post_param( 'id' ); $data = vc_post_param( 'data' ); $shortcode = new Vc_Automap_Model( $id ); if ( ! isset( $data['params'] ) ) $data['params'] = array(); $shortcode->set( $data ); return $shortcode->save(); } public function delete() { $id = vc_post_param( 'id' ); $shortcode = new Vc_Automap_Model( $id ); return $shortcode->delete(); } public function read() { return Vc_Automap_Model::findAll(); } // }} /** * Ajax result output */ function result( $data ) { echo is_array( $data ) || is_object( $data ) ? json_encode( $data ) : $data; die(); } /** * Setter/Getter for Disabling Automapper * * @static * @param bool $disable */ // {{ public static function setDisabled( $disable = true ) { self::$disabled = $disable; } public static function disabled() { return self::$disabled; } // }} /** * Setter/Getter for Automapper title * * @static * @param string $title */ // {{ public function setTitle( $title ) { $this->title = $title; } public function title() { return $this->title; } // }} public static function map() { $shortcodes = Vc_Automap_Model::findAll(); if(!empty($shortcodes)) foreach ( $shortcodes as $shortcode ) { vc_map( array( "name" => $shortcode->name, "base" => $shortcode->tag, "category" => vc_atm_build_categories_array( $shortcode->category ), "description" => $shortcode->description, "params" => vc_atm_build_params_array( $shortcode->params ), "show_settings_on_create" => ! empty( $shortcode->params ), "atm" => true, "icon" => 'icon-wpb-atm' ) ); } } } } // Helpers if ( ! function_exists( 'vc_atm_build_categories_array' ) ) { function vc_atm_build_categories_array( $string ) { return array_map( 'vc_atm_textdomain_category', explode( ',', preg_replace( '/\,\s+/', ',', trim( $string ) ) ) ); } function vc_atm_textdomain_category( $value ) { return __( $value, 'js_composer' ); } } if ( ! function_exists( 'vc_atm_build_params_array' ) ) { function vc_atm_build_params_array( $array ) { $params = array(); if ( is_array( $array ) ) { foreach ( $array as $param ) { if ( $param['type'] === 'dropdown' ) $param['value'] = explode( ',', preg_replace( '/\,\s+/', ',', trim( $param['value'] ) ) ); $params[] = $param; } } return $params; } }