PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/enfold/config-templatebuilder/avia-template-builder/php/generic-helper.class.php

https://gitlab.com/haque.mdmanzurul/soundkreationsfinal
PHP | 367 lines | 223 code | 83 blank | 61 comment | 45 complexity | 4fdfa268d0065b6f25be20e0630b47da MD5 | raw file
  1. <?php
  2. /**
  3. * Central AviaHelper class which holds quite a few unrelated functions
  4. */
  5. // Don't load directly
  6. if ( !defined('ABSPATH') ) { die('-1'); }
  7. if ( !class_exists( 'AviaHelper' ) ) {
  8. class AviaHelper
  9. {
  10. static $cache = array(); //holds database requests or results of complex functions
  11. static $templates = array(); //an array that holds all the templates that should be created when the print_media_templates hook is called
  12. /**
  13. * get_url - Returns a url based on a string that holds either post type and id or taxonomy and id
  14. */
  15. static function get_url($link, $post_id = false)
  16. {
  17. $link = explode(',', $link);
  18. if($link[0] == 'lightbox')
  19. {
  20. $link = wp_get_attachment_image_src($post_id, apply_filters('avf_avia_builder_helper_lightbox_size','large'));
  21. return $link[0];
  22. }
  23. if(empty($link[1])) return $link[0];
  24. if($link[0] == 'manually') return $link[1];
  25. if(post_type_exists( $link[0] )) return get_permalink($link[1]);
  26. if(taxonomy_exists( $link[0] ))
  27. {
  28. $return = get_term_link(get_term($link[1], $link[0]));
  29. if(is_object($return)) $return = ""; //if an object is returned it is a WP_Error object and something was not found
  30. return $return;
  31. }
  32. }
  33. /**
  34. * get_entry - fetches an entry based on a post type and id
  35. */
  36. static function get_entry($entry)
  37. {
  38. $entry = explode(',', $entry);
  39. if(empty($entry[1])) return false;
  40. if($entry[0] == 'manually') return false;
  41. if(post_type_exists( $entry[0] )) return get_post($entry[1]);
  42. }
  43. /**
  44. * fetch all available sidebars
  45. */
  46. static function get_registered_sidebars($sidebars = array(), $exclude = array())
  47. {
  48. //fetch all registered sidebars and save them to the sidebars array
  49. global $wp_registered_sidebars;
  50. foreach($wp_registered_sidebars as $sidebar)
  51. {
  52. if( !in_array($sidebar['name'], $exclude))
  53. {
  54. $sidebars[$sidebar['name']] = $sidebar['name'];
  55. }
  56. }
  57. return $sidebars;
  58. }
  59. static function get_registered_image_sizes($exclude = array(), $enforce_both = false, $exclude_default = false)
  60. {
  61. global $_wp_additional_image_sizes;
  62. // Standard sizes
  63. $image_sizes = array( 'no scaling'=> array("width"=>"Original Width ", "height"=>" Original Height"),
  64. 'thumbnail' => array("width"=>get_option('thumbnail_size_w'), "height"=>get_option('thumbnail_size_h')),
  65. 'medium' => array("width"=>get_option('medium_size_w'), "height"=>get_option('medium_size_h')),
  66. 'large' => array("width"=>get_option('large_size_w'), "height"=>get_option('large_size_h')));
  67. if(!empty($exclude_default)) unset($image_sizes['no scaling']);
  68. if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
  69. $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
  70. $result = array();
  71. foreach($image_sizes as $key => $image)
  72. {
  73. if( (is_array($exclude) && !in_array($key, $exclude)) || (is_numeric($exclude) && ($image['width'] > $exclude || $image['height'] > $exclude)) || !is_numeric($image['height']))
  74. {
  75. if($enforce_both == true && is_numeric($image['height']))
  76. {
  77. if($image['width'] < $exclude || $image['height'] < $exclude) continue;
  78. }
  79. $title = str_replace("_",' ', $key) ." (".$image['width']."x".$image['height'].")";
  80. $result[ucwords( $title )] = $key;
  81. }
  82. }
  83. return $result;
  84. }
  85. static function list_menus()
  86. {
  87. $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
  88. $result = array();
  89. if(!empty($menus))
  90. {
  91. foreach ($menus as $menu)
  92. {
  93. $result[$menu->name] = $menu->term_taxonomy_id;
  94. }
  95. }
  96. return $result;
  97. }
  98. /**
  99. * is_ajax - Returns true when the page is loaded via ajax.
  100. */
  101. static function is_ajax()
  102. {
  103. if ( defined('DOING_AJAX') )
  104. return true;
  105. return ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) ? true : false;
  106. }
  107. /**
  108. * function that gets called on backend pages and hooks other functions into wordpress
  109. *
  110. * @return void
  111. */
  112. static function backend()
  113. {
  114. add_action( 'print_media_templates', array('AviaHelper', 'print_templates' )); //create js templates for AviaBuilder Canvas Elements
  115. }
  116. /**
  117. * Helper function that prints an array as js object. can call itself in case of nested arrays
  118. *
  119. * @return void
  120. */
  121. static function print_javascript($objects = array(), $print = true, $passed = "")
  122. {
  123. $output = "";
  124. if($print) $output .= "\n<script type='text/javascript' class='av-php-sent-to-frontend'>/* <![CDATA[ */ \n";
  125. foreach($objects as $key => $object)
  126. {
  127. if(is_array($object))
  128. {
  129. if(empty($passed))
  130. {
  131. $output .= "var {$key} = {};\n";
  132. $pass = $key;
  133. }
  134. else
  135. {
  136. $output .= "{$passed}['{$key}'] = {};\n";
  137. $pass = "{$passed}['{$key}']";
  138. }
  139. $output .= AviaHelper::print_javascript($object, false, $pass);
  140. }
  141. else
  142. {
  143. if(!is_numeric($object) && !is_bool($object)) $object = json_encode($object);
  144. if(empty($object)) $object = "false";
  145. if(empty($passed))
  146. {
  147. $output .= "var {$key} = {$object};\n";
  148. }
  149. else
  150. {
  151. $output .= "{$passed}['{$key}'] = {$object};\n";
  152. }
  153. }
  154. }
  155. if($print)
  156. {
  157. $output .= "\n /* ]]> */</script>\n\n";
  158. echo $output;
  159. }
  160. return $output;
  161. }
  162. /**
  163. * Helper function that prints all the javascript templates
  164. *
  165. * @return void
  166. */
  167. static function print_templates()
  168. {
  169. foreach (self::$templates as $key => $template)
  170. {
  171. echo "\n<script type='text/html' id='avia-tmpl-{$key}'>\n";
  172. echo $template;
  173. echo "\n</script>\n\n";
  174. }
  175. //reset the array
  176. self::$templates = array();
  177. }
  178. /**
  179. * Helper function that creates a new javascript template to be called
  180. *
  181. * @return void
  182. */
  183. static function register_template($key, $html)
  184. {
  185. self::$templates[$key] = $html;
  186. }
  187. /**
  188. * Helper function that fetches all "public" post types.
  189. *
  190. * @return array $post_types example output: data-modal='true'
  191. */
  192. static function public_post_types()
  193. {
  194. $post_types = get_post_types(array('public' => false, 'name' => 'attachment', 'show_ui'=>false, 'publicly_queryable'=>false), 'names', 'NOT');
  195. $post_types['page'] = 'page';
  196. $post_types = array_map("ucfirst", $post_types);
  197. $post_types = apply_filters('avia_public_post_types', $post_types);
  198. self::$cache['post_types'] = $post_types;
  199. return $post_types;
  200. }
  201. /**
  202. * Helper function that fetches all taxonomies attached to public post types.
  203. *
  204. * @return array $taxonomies
  205. */
  206. static function public_taxonomies($post_types = false, $merged = false)
  207. {
  208. $taxonomies = array();
  209. if(!$post_types)
  210. $post_types = empty(self::$cache['post_types']) ? self::public_post_types() : self::$cache['post_types'];
  211. if(!is_array($post_types))
  212. $post_types = array($post_types => ucfirst($post_types));
  213. foreach($post_types as $type => $post)
  214. {
  215. $taxonomies[$type] = get_object_taxonomies($type);
  216. }
  217. $taxonomies = apply_filters('avia_public_taxonomies', $taxonomies);
  218. self::$cache['taxonomies'] = $taxonomies;
  219. if($merged)
  220. {
  221. $new = array();
  222. foreach($taxonomies as $taxonomy)
  223. {
  224. foreach($taxonomy as $tax)
  225. {
  226. $new[$tax] = ucwords(str_replace("_", " ",$tax));
  227. }
  228. }
  229. $taxonomies = $new;
  230. }
  231. return $taxonomies;
  232. }
  233. /**
  234. * Helper function that converts an array into a html data string
  235. *
  236. * @param array $data example input: array('modal'=>'true')
  237. * @return string $data_string example output: data-modal='true'
  238. */
  239. static function create_data_string($data = array())
  240. {
  241. $data_string = "";
  242. foreach($data as $key=>$value)
  243. {
  244. if(is_array($value)) $value = implode(", ",$value);
  245. $data_string .= " data-$key='$value' ";
  246. }
  247. return $data_string;
  248. }
  249. /**
  250. * Create a lower case version of a string without spaces so we can use that string for database settings
  251. *
  252. * @param string $string to convert
  253. * @return string the converted string
  254. */
  255. static function save_string( $string , $replace = "_")
  256. {
  257. $string = strtolower($string);
  258. $trans = array(
  259. '&\#\d+?;' => '',
  260. '&\S+?;' => '',
  261. '\s+' => $replace,
  262. 'ä' => 'ae',
  263. 'ö' => 'oe',
  264. 'ü' => 'ue',
  265. 'Ä' => 'Ae',
  266. 'Ö' => 'Oe',
  267. 'Ü' => 'Ue',
  268. 'ß' => 'ss',
  269. '[^a-z0-9\-\._]' => '',
  270. $replace.'+' => $replace,
  271. $replace.'$' => $replace,
  272. '^'.$replace => $replace,
  273. '\.+$' => ''
  274. );
  275. $trans = apply_filters('avf_save_string_translations', $trans, $string, $replace);
  276. $string = strip_tags($string);
  277. foreach ($trans as $key => $val)
  278. {
  279. $string = preg_replace("#".$key."#i", $val, $string);
  280. }
  281. return stripslashes($string);
  282. }
  283. }
  284. }