class-vc-automap-model.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. if ( ! class_exists( 'Vc_Automap_Model' ) ) {
  6. /**
  7. * Shortcode as model for automapper. Provides crud functionality for storing data for shortcodes that mapped by ATM
  8. *
  9. * @see Vc_Automapper
  10. * @since 4.1
  11. */
  12. class Vc_Automap_Model {
  13. /**
  14. * @var string
  15. */
  16. protected static $option_name = 'vc_automapped_shortcodes';
  17. /**
  18. * @var
  19. */
  20. protected static $option_data;
  21. /**
  22. * @var array|bool
  23. */
  24. public $id = false;
  25. public $tag;
  26. /**
  27. * @var mixed
  28. */
  29. protected $data;
  30. /**
  31. * @var array
  32. */
  33. protected $vars = array(
  34. 'tag',
  35. 'name',
  36. 'category',
  37. 'description',
  38. 'params',
  39. );
  40. public $name;
  41. /**
  42. * @param $data
  43. */
  44. public function __construct( $data ) {
  45. $this->loadOptionData();
  46. $this->id = is_array( $data ) && isset( $data['id'] ) ? esc_attr( $data['id'] ) : $data;
  47. if ( is_array( $data ) ) {
  48. $this->data = stripslashes_deep( $data );
  49. }
  50. foreach ( $this->vars as $var ) {
  51. $this->{$var} = $this->get( $var );
  52. }
  53. }
  54. /**
  55. * @return array
  56. */
  57. public static function findAll() {
  58. self::loadOptionData();
  59. $records = array();
  60. foreach ( self::$option_data as $id => $record ) {
  61. $record['id'] = $id;
  62. $model = new self( $record );
  63. if ( $model ) {
  64. $records[] = $model;
  65. }
  66. }
  67. return $records;
  68. }
  69. /**
  70. * @return array|mixed
  71. */
  72. final protected static function loadOptionData() {
  73. if ( is_null( self::$option_data ) ) {
  74. self::$option_data = get_option( self::$option_name );
  75. }
  76. if ( ! self::$option_data ) {
  77. self::$option_data = array();
  78. }
  79. return self::$option_data;
  80. }
  81. /**
  82. * @param $key
  83. *
  84. * @return null
  85. */
  86. public function get( $key ) {
  87. if ( is_null( $this->data ) ) {
  88. $this->data = isset( self::$option_data[ $this->id ] ) ? self::$option_data[ $this->id ] : array();
  89. }
  90. return isset( $this->data[ $key ] ) ? $this->data[ $key ] : null;
  91. }
  92. /**
  93. * @param $attr
  94. * @param null $value
  95. */
  96. public function set( $attr, $value = null ) {
  97. if ( is_array( $attr ) ) {
  98. foreach ( $attr as $key => $value ) {
  99. $this->set( $key, $value );
  100. }
  101. } elseif ( ! is_null( $value ) ) {
  102. $this->{$attr} = $value;
  103. }
  104. }
  105. /**
  106. * @return bool
  107. */
  108. public function save() {
  109. if ( ! $this->isValid() ) {
  110. return false;
  111. }
  112. foreach ( $this->vars as $var ) {
  113. $this->data[ $var ] = $this->{$var};
  114. }
  115. return $this->saveOption();
  116. }
  117. /**
  118. * @return bool
  119. */
  120. public function delete() {
  121. return $this->deleteOption();
  122. }
  123. /**
  124. * @return bool
  125. */
  126. public function isValid() {
  127. if ( ! is_string( $this->name ) || empty( $this->name ) ) {
  128. return false;
  129. }
  130. if ( ! preg_match( '/^\S+$/', $this->tag ) ) {
  131. return false;
  132. }
  133. return true;
  134. }
  135. /**
  136. * @return bool
  137. */
  138. protected function saveOption() {
  139. self::$option_data[ $this->id ] = $this->data;
  140. return update_option( self::$option_name, self::$option_data );
  141. }
  142. /**
  143. * @return bool
  144. */
  145. protected function deleteOption() {
  146. unset( self::$option_data[ $this->id ] );
  147. return delete_option( self::$option_name );
  148. }
  149. }
  150. }