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 );
}
}
/**
* @return array
*/
static function findAll() {
self::loadOptionData();
$records = array();
foreach ( self::$option_data as $id => $record ) {
$record['id'] = $id;
$model = new self( $record );
if ( $model ) {
$records[] = $model;
}
}
return $records;
}
/**
* @return array|mixed|void
*/
final protected static function loadOptionData() {
if ( is_null( self::$option_data ) ) {
self::$option_data = get_option( self::$option_name );
}
if ( ! self::$option_data ) {
self::$option_data = array();
}
return self::$option_data;
}
/**
* @param $key
*
* @return null
*/
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;
}
/**
* @param $attr
* @param null $value
*/
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;
}
}
/**
* @return bool
*/
function save() {
if ( ! $this->isValid() ) {
return false;
}
foreach ( $this->vars as $var ) {
$this->data[ $var ] = $this->$var;
}
return $this->saveOption();
}
/**
* @return bool
*/
function delete() {
return $this->deleteOption();
}
/**
* @return bool
*/
public function isValid() {
if ( ! is_string( $this->name ) || empty( $this->name ) ) {
return false;
}
if ( ! preg_match( '/^\S+$/', $this->tag ) ) {
return false;
}
return true;
}
/**
* @return bool
*/
protected function saveOption() {
self::$option_data[ $this->id ] = $this->data;
return update_option( self::$option_name, self::$option_data );
}
/**
* @return bool
*/
protected function deleteOption() {
unset( self::$option_data[ $this->id ] );
return update_option( 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 {
/**
* @var bool
*/
protected static $disabled = false;
/**
*
*/
public function __construct() {
$this->title = __( 'Shortcode Mapper', 'js_composer' );
}
/**
*
*/
public function addAjaxActions() {
add_action( 'wp_ajax_vc_automapper', array( &$this, 'goAction' ) );
return $this;
}
// 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() {
vc_user_access()
->checkAdminNonce()
->validateDie()
->wpAny( 'manage_options' )
->validateDie()
->part( 'settings' )
->can( 'vc-automapper-tab' )
->validateDie();
$action = vc_post_param( 'vc_action' );
$this->result( $this->$action() );
}
/**
* @return bool
*/
public function create() {
$data = vc_post_param( 'data' );
$shortcode = new Vc_Automap_Model( $data );
return $shortcode->save();
}
/**
* @return bool
*/
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();
}
/**
* @return bool
*/
public function delete() {
$id = vc_post_param( 'id' );
$shortcode = new Vc_Automap_Model( $id );
return $shortcode->delete();
}
/**
* @return array
*/
public function read() {
return Vc_Automap_Model::findAll();
}
// }}
/**
* Ajax result output
*
* @param $data
*/
function result( $data ) {
echo is_array( $data ) || is_object( $data ) ? json_encode( $data ) : $data;
die();
}
/**
* Setter/Getter for Disabling Automapper
*
* @static
*
* @param bool $disable
*/
// {{
/**
* @param bool $disable
*/
public static function setDisabled( $disable = true ) {
self::$disabled = $disable;
}
/**
* @return bool
*/
public static function disabled() {
return self::$disabled;
}
// }}
/**
* Setter/Getter for Automapper title
*
* @static
*
* @param string $title
*/
// {{
/**
* @param string $title
*/
public function setTitle( $title ) {
$this->title = $title;
}
/**
* @return string|void
*/
public function title() {
return $this->title;
}
// }}
/**
*
*/
public static function map() {
$shortcodes = Vc_Automap_Model::findAll();
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' ) ) {
/**
* @param $string
*
* @return array
*/
function vc_atm_build_categories_array( $string ) {
return explode( ',', preg_replace( '/\,\s+/', ',', trim( $string ) ) );
}
}
if ( ! function_exists( 'vc_atm_build_params_array' ) ) {
/**
* @param $array
*
* @return array
*/
function vc_atm_build_params_array( $array ) {
$params = array();
if ( is_array( $array ) ) {
foreach ( $array as $param ) {
if ( 'dropdown' === $param['type'] ) {
$param['value'] = explode( ',', preg_replace( '/\,\s+/', ',', trim( $param['value'] ) ) );
}
$param['save_always'] = true;
$params[] = $param;
}
}
return $params;
}
}