PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/LayerSlider/classes/class.ls.sliders.php

https://gitlab.com/webkod3r/tripolis
PHP | 409 lines | 182 code | 102 blank | 125 comment | 31 complexity | 6b1671102b1f45dfebde7ef603a8cb73 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(), $slug = '') {
  121. global $wpdb;
  122. // Slider data
  123. $data = !empty($data) ? $data : array(
  124. 'properties' => array('title' => $title),
  125. 'layers' => array(array()),
  126. );
  127. // Fix WP 4.2 issue with longer varchars
  128. // than the column length
  129. if(strlen($title) > 99) {
  130. $title = substr($title, 0, (99-strlen($title)) );
  131. }
  132. // Insert slider, WPDB will escape data automatically
  133. $wpdb->insert($wpdb->prefix.LS_DB_TABLE, array(
  134. 'author' => get_current_user_id(),
  135. 'name' => $title,
  136. 'slug' => $slug,
  137. 'data' => json_encode($data),
  138. 'date_c' => time(),
  139. 'date_m' => time()
  140. ), array(
  141. '%d', '%s', '%s', '%s', '%d', '%d'
  142. ));
  143. // Return insert database ID
  144. return $wpdb->insert_id;
  145. }
  146. /**
  147. * Updates sliders
  148. *
  149. * @since 5.2.0
  150. * @access public
  151. * @param int $id The database ID of the slider to be updated
  152. * @param string $title The new title of the slider
  153. * @param array $data The new settings of the slider
  154. * @return bool Returns true on success, false otherwise
  155. */
  156. public static function update($id = 0, $title = 'Unnamed', $data = array(), $slug = '') {
  157. global $wpdb;
  158. // Slider data
  159. $data = !empty($data) ? $data : array(
  160. 'properties' => array('title' => $title),
  161. 'layers' => array(array()),
  162. );
  163. // Fix WP 4.2 issue with longer varchars
  164. // than the column length
  165. if(strlen($title) > 99) {
  166. $title = substr($title, 0, (99-strlen($title)) );
  167. }
  168. // Insert slider, WPDB will escape data automatically
  169. $wpdb->update($wpdb->prefix.LS_DB_TABLE, array(
  170. 'name' => $title,
  171. 'slug' => $slug,
  172. 'data' => json_encode($data),
  173. 'date_m' => time()
  174. ),
  175. array('id' => $id),
  176. array('%s', '%s', '%s', '%d')
  177. );
  178. // Return insert database ID
  179. return true;
  180. }
  181. /**
  182. * Marking a slider as removed without deleting it
  183. * with its database ID.
  184. *
  185. * @since 5.0.0
  186. * @access public
  187. * @param int $id The database ID if the slider to remove
  188. * @return bool Returns true on success, false otherwise
  189. */
  190. public static function remove($id = null) {
  191. // Check ID
  192. if(!is_int($id)) { return false; }
  193. // Remove
  194. global $wpdb;
  195. $wpdb->update($wpdb->prefix.LS_DB_TABLE,
  196. array('flag_deleted' => 1),
  197. array('id' => $id),
  198. '%d', '%d'
  199. );
  200. return true;
  201. }
  202. /**
  203. * Delete a slider by its database ID
  204. *
  205. * @since 5.0.0
  206. * @access public
  207. * @param int $id The database ID if the slider to delete
  208. * @return bool Returns true on success, false otherwise
  209. */
  210. public static function delete($id = null) {
  211. // Check ID
  212. if(!is_int($id)) { return false; }
  213. // Delete
  214. global $wpdb;
  215. $wpdb->delete($wpdb->prefix.LS_DB_TABLE, array('id' => $id), '%d');
  216. return true;
  217. }
  218. /**
  219. * Restore a slider marked as removed previously by its database ID.
  220. *
  221. * @since 5.0.0
  222. * @access public
  223. * @param int $id The database ID if the slider to restore
  224. * @return bool Returns true on success, false otherwise
  225. */
  226. public static function restore($id = null) {
  227. // Check ID
  228. if(!is_int($id)) { return false; }
  229. // Remove
  230. global $wpdb;
  231. $wpdb->update($wpdb->prefix.LS_DB_TABLE,
  232. array('flag_deleted' => 0),
  233. array('id' => $id),
  234. '%d', '%d'
  235. );
  236. return true;
  237. }
  238. private static function _getById($id = null) {
  239. // Check ID
  240. if(!is_int($id)) { return false; }
  241. // Get Sliders
  242. global $wpdb;
  243. $table = $wpdb->prefix.LS_DB_TABLE;
  244. $result = $wpdb->get_row("SELECT * FROM $table WHERE id = '$id' ORDER BY id DESC LIMIT 1", ARRAY_A);
  245. // Check return value
  246. if(!is_array($result)) { return false; }
  247. // Return result
  248. $result['data'] = json_decode($result['data'], true);
  249. return $result;
  250. }
  251. private static function _getByIds($ids = null) {
  252. // Check ID
  253. if(!is_array($ids)) { return false; }
  254. // DB stuff
  255. global $wpdb;
  256. $table = $wpdb->prefix.LS_DB_TABLE;
  257. $limit = count($ids);
  258. // Collect IDs
  259. if(is_array($ids) && !empty($ids)) {
  260. $tmp = array();
  261. foreach($ids as $id) {
  262. $tmp[] = 'id = \''.intval($id).'\'';
  263. }
  264. $ids = implode(' OR ', $tmp);
  265. unset($tmp);
  266. }
  267. // Make the call
  268. $result = $wpdb->get_results("SELECT * FROM $table WHERE $ids ORDER BY id DESC LIMIT $limit", ARRAY_A);
  269. // Decode slider data
  270. if(is_array($result) && !empty($result)) {
  271. foreach($result as $key => $slider) {
  272. $result[$key]['data'] = json_decode($slider['data'], true);
  273. }
  274. return $result;
  275. // Failed query
  276. } else {
  277. return false;
  278. }
  279. }
  280. private static function _getBySlug($slug) {
  281. // Check slug
  282. if(empty($slug)) { return false; }
  283. else { $slug = esc_sql($slug); }
  284. // Get DB stuff
  285. global $wpdb;
  286. $table = $wpdb->prefix.LS_DB_TABLE;
  287. // Make the call
  288. $result = $wpdb->get_row("SELECT * FROM $table WHERE slug = '$slug' ORDER BY id DESC LIMIT 1", ARRAY_A);
  289. // Check return value
  290. if(!is_array($result)) { return false; }
  291. // Return result
  292. $result['data'] = json_decode($result['data'], true);
  293. return $result;
  294. }
  295. private static function _getRandom() {
  296. // Get DB stuff
  297. global $wpdb;
  298. $table = $wpdb->prefix.LS_DB_TABLE;
  299. // Make the call
  300. $result = $wpdb->get_row("SELECT * FROM $table WHERE flag_hidden = '0' AND flag_deleted = '0' ORDER BY RAND() LIMIT 1", ARRAY_A);
  301. // Check return value
  302. if(!is_array($result)) { return false; }
  303. // Return result
  304. $result['data'] = json_decode($result['data'], true);
  305. return $result;
  306. }
  307. }