/wp-content/themes/enfold/config-layerslider/LayerSlider/classes/class.ls.sliders.php

https://gitlab.com/haque.mdmanzurul/soundkreationsfinal · PHP · 397 lines · 176 code · 100 blank · 121 comment · 29 complexity · 4223bcccc5aa05259a30a86467ab3fb0 MD5 · raw file

  1. <?php
  2. class LS_Sliders {
  3. /**
  4. * @var array $results Array containing the result of the last DB query
  5. * @access public
  6. */
  7. public static $results = array();
  8. /**
  9. * @var int $count Count of found sliders in the last DB query
  10. * @access public
  11. */
  12. public static $count = null;
  13. /**
  14. * Private constructor to prevent instantiate static class
  15. *
  16. * @since 5.0.0
  17. * @access private
  18. * @return void
  19. */
  20. private function __construct() {
  21. }
  22. /**
  23. * Returns the count of found sliders in the last DB query
  24. *
  25. * @since 5.0.0
  26. * @access public
  27. * @return int Count of found sliders in the last DB query
  28. */
  29. public static function count() {
  30. return self::$count;
  31. }
  32. /**
  33. * Find sliders with the provided filters
  34. *
  35. * @since 5.0.0
  36. * @access public
  37. * @param mixed $args Find any slider with the provided filters
  38. * @return mixed Array on success, false otherwise
  39. */
  40. public static function find($args = array()) {
  41. // Find by slider ID
  42. if(is_numeric($args) && intval($args) == $args) {
  43. return self::_getById( (int) $args );
  44. // Random slider
  45. } elseif($args === 'random') {
  46. return self::_getRandom();
  47. // Find by slider slug
  48. } elseif(is_string($args)) {
  49. return self::_getBySlug($args);
  50. // Find by list of slider IDs
  51. } elseif(is_array($args) && isset($args[0]) && is_numeric($args[0])) {
  52. return self::_getByIds($args);
  53. // Find by query
  54. } else {
  55. // Defaults
  56. $defaults = array(
  57. 'columns' => '*',
  58. 'where' => '',
  59. 'exclude' => array('hidden', 'removed'),
  60. 'orderby' => 'date_c',
  61. 'order' => 'DESC',
  62. 'limit' => 10,
  63. 'page' => 1,
  64. 'data' => true
  65. );
  66. // User data
  67. foreach($defaults as $key => $val) {
  68. if(!isset($args[$key])) { $args[$key] = $val; } }
  69. // Escape user data
  70. foreach($args as $key => $val) {
  71. $args[$key] = esc_sql($val); }
  72. // Exclude
  73. if(!empty($args['exclude'])) {
  74. if(in_array('hidden', $args['exclude'])) {
  75. $exclude[] = "flag_hidden = '0'"; }
  76. if(in_array('removed', $args['exclude'])) {
  77. $exclude[] = "flag_deleted = '0'"; }
  78. $args['exclude'] = implode(' AND ', $exclude);
  79. }
  80. // Where
  81. $where = '';
  82. if(!empty($args['where']) && !empty($args['exclude'])) {
  83. $where = "WHERE ({$args['exclude']}) AND ({$args['where']}) ";
  84. } elseif(!empty($args['where'])) {
  85. $where = "WHERE {$args['where']} ";
  86. } elseif(!empty($args['exclude'])) {
  87. $where = "WHERE {$args['exclude']} ";
  88. }
  89. // Some adjustments
  90. $args['limit'] = ($args['limit'] * $args['page'] - $args['limit']).', '.$args['limit'];
  91. // Build the query
  92. global $wpdb;
  93. $table = $wpdb->prefix.LS_DB_TABLE;
  94. $sliders = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS {$args['columns']} FROM $table $where
  95. ORDER BY {$args['orderby']} {$args['order']} LIMIT {$args['limit']}", ARRAY_A);
  96. // Set counter
  97. $found = $wpdb->get_col("SELECT FOUND_ROWS()");
  98. self::$count = (int) $found[0];
  99. // Return original value on error
  100. if(!is_array($sliders)) { return $sliders; };
  101. // Parse slider data
  102. if($args['data']) {
  103. foreach($sliders as $key => $val) {
  104. $sliders[$key]['data'] = json_decode($val['data'], true);
  105. }
  106. }
  107. // Return sliders
  108. return $sliders;
  109. }
  110. }
  111. /**
  112. * Add slider with the provided name and optional slider data
  113. *
  114. * @since 5.0.0
  115. * @access public
  116. * @param string $title The title of the slider to create
  117. * @param array $data The settings of the slider to create
  118. * @return int The slider database ID inserted
  119. */
  120. public static function add($title = 'Unnamed', $data = array()) {
  121. global $wpdb;
  122. // Slider data
  123. $data = !empty($data) ? $data : array(
  124. 'properties' => array('title' => $title),
  125. 'layers' => array(array()),
  126. );
  127. // Insert slider, WPDB will escape data automatically
  128. $wpdb->insert($wpdb->prefix.LS_DB_TABLE, array(
  129. 'author' => get_current_user_id(),
  130. 'name' => $title,
  131. 'slug' => '',
  132. 'data' => json_encode($data),
  133. 'date_c' => time(),
  134. 'date_m' => time()
  135. ), array(
  136. '%d', '%s', '%s', '%s', '%d', '%d'
  137. ));
  138. // Return insert database ID
  139. return $wpdb->insert_id;
  140. }
  141. /**
  142. * Updates sliders
  143. *
  144. * @since 5.2.0
  145. * @access public
  146. * @param int $id The database ID of the slider to be updated
  147. * @param string $title The new title of the slider
  148. * @param array $data The new settings of the slider
  149. * @return bool Returns true on success, false otherwise
  150. */
  151. public static function update($id = 0, $title = 'Unnamed', $data = array(), $slug = '') {
  152. global $wpdb;
  153. // Slider data
  154. $data = !empty($data) ? $data : array(
  155. 'properties' => array('title' => $title),
  156. 'layers' => array(array()),
  157. );
  158. // Insert slider, WPDB will escape data automatically
  159. $wpdb->update($wpdb->prefix.LS_DB_TABLE, array(
  160. 'name' => $title,
  161. 'slug' => $slug,
  162. 'data' => json_encode($data),
  163. 'date_m' => time()
  164. ),
  165. array('id' => $id),
  166. array('%s', '%s', '%s', '%d')
  167. );
  168. // Return insert database ID
  169. return true;
  170. }
  171. /**
  172. * Marking a slider as removed without deleting it
  173. * with its database ID.
  174. *
  175. * @since 5.0.0
  176. * @access public
  177. * @param int $id The database ID if the slider to remove
  178. * @return bool Returns true on success, false otherwise
  179. */
  180. public static function remove($id = null) {
  181. // Check ID
  182. if(!is_int($id)) { return false; }
  183. // Remove
  184. global $wpdb;
  185. $wpdb->update($wpdb->prefix.LS_DB_TABLE,
  186. array('flag_deleted' => 1),
  187. array('id' => $id),
  188. '%d', '%d'
  189. );
  190. return true;
  191. }
  192. /**
  193. * Delete a slider by its database ID
  194. *
  195. * @since 5.0.0
  196. * @access public
  197. * @param int $id The database ID if the slider to delete
  198. * @return bool Returns true on success, false otherwise
  199. */
  200. public static function delete($id = null) {
  201. // Check ID
  202. if(!is_int($id)) { return false; }
  203. // Delete
  204. global $wpdb;
  205. $wpdb->delete($wpdb->prefix.LS_DB_TABLE, array('id' => $id), '%d');
  206. return true;
  207. }
  208. /**
  209. * Restore a slider marked as removed previously by its database ID.
  210. *
  211. * @since 5.0.0
  212. * @access public
  213. * @param int $id The database ID if the slider to restore
  214. * @return bool Returns true on success, false otherwise
  215. */
  216. public static function restore($id = null) {
  217. // Check ID
  218. if(!is_int($id)) { return false; }
  219. // Remove
  220. global $wpdb;
  221. $wpdb->update($wpdb->prefix.LS_DB_TABLE,
  222. array('flag_deleted' => 0),
  223. array('id' => $id),
  224. '%d', '%d'
  225. );
  226. return true;
  227. }
  228. private static function _getById($id = null) {
  229. // Check ID
  230. if(!is_int($id)) { return false; }
  231. // Get Sliders
  232. global $wpdb;
  233. $table = $wpdb->prefix.LS_DB_TABLE;
  234. $result = $wpdb->get_row("SELECT * FROM $table WHERE id = '$id' ORDER BY id DESC LIMIT 1", ARRAY_A);
  235. // Check return value
  236. if(!is_array($result)) { return false; }
  237. // Return result
  238. $result['data'] = json_decode($result['data'], true);
  239. return $result;
  240. }
  241. private static function _getByIds($ids = null) {
  242. // Check ID
  243. if(!is_array($ids)) { return false; }
  244. // DB stuff
  245. global $wpdb;
  246. $table = $wpdb->prefix.LS_DB_TABLE;
  247. $limit = count($ids);
  248. // Collect IDs
  249. if(is_array($ids) && !empty($ids)) {
  250. $tmp = array();
  251. foreach($ids as $id) {
  252. $tmp[] = 'id = \''.intval($id).'\'';
  253. }
  254. $ids = implode(' OR ', $tmp);
  255. unset($tmp);
  256. }
  257. // Make the call
  258. $result = $wpdb->get_results("SELECT * FROM $table WHERE $ids ORDER BY id DESC LIMIT $limit", ARRAY_A);
  259. // Decode slider data
  260. if(is_array($result) && !empty($result)) {
  261. foreach($result as $key => $slider) {
  262. $result[$key]['data'] = json_decode($slider['data'], true);
  263. }
  264. return $result;
  265. // Failed query
  266. } else {
  267. return false;
  268. }
  269. }
  270. private static function _getBySlug($slug) {
  271. // Check slug
  272. if(empty($slug)) { return false; }
  273. else { $slug = esc_sql($slug); }
  274. // Get DB stuff
  275. global $wpdb;
  276. $table = $wpdb->prefix.LS_DB_TABLE;
  277. // Make the call
  278. $result = $wpdb->get_row("SELECT * FROM $table WHERE slug = '$slug' ORDER BY id DESC LIMIT 1", ARRAY_A);
  279. // Check return value
  280. if(!is_array($result)) { return false; }
  281. // Return result
  282. $result['data'] = json_decode($result['data'], true);
  283. return $result;
  284. }
  285. private static function _getRandom() {
  286. // Get DB stuff
  287. global $wpdb;
  288. $table = $wpdb->prefix.LS_DB_TABLE;
  289. // Make the call
  290. $result = $wpdb->get_row("SELECT * FROM $table WHERE flag_hidden = '0' AND flag_deleted = '0' ORDER BY RAND() LIMIT 1", ARRAY_A);
  291. // Check return value
  292. if(!is_array($result)) { return false; }
  293. // Return result
  294. $result['data'] = json_decode($result['data'], true);
  295. return $result;
  296. }
  297. }