PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/gs-basic-functions/gs-basic-functions.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 535 lines | 363 code | 55 blank | 117 comment | 59 complexity | bc19bac4627fcf66389eddddae40a6db MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: GoSeese Basic Functions
  4. Plugin URI: http://www.goseese.com/
  5. Description: This plugin doesn't do anything by it's self. It's a collection of common functions that are used through out my plugins.
  6. Version: alpha 0.8
  7. Author: Jeff Seese
  8. Author URI: http://www.goseese.com
  9. License: Purchase only
  10. */
  11. /*
  12. ==================================================================
  13. Require Current Version of Jquery, this is necessary for
  14. parseJSON in 1.4.x
  15. ================================================================== */
  16. /*
  17. .8 5/31/2010 changed jquery current version check/force to not execute on page/post edit. The tiny MCE does not funciton with jquery 1.4.2
  18. */
  19. if (!function_exists('current_jquery') )
  20. {
  21. function current_jquery($version)
  22. {
  23. if ($_GET['action'] === 'edit' && is_admin() ) { return; }
  24. global $wp_scripts;
  25. if ( ( version_compare($version, $wp_scripts -> registered[jquery] -> ver) == 1 ) ) {
  26. wp_deregister_script('jquery');
  27. wp_register_script('jquery',
  28. 'http://ajax.googleapis.com/ajax/libs/jquery/'.$version.'/jquery.min.js',
  29. false, $version);
  30. }
  31. }
  32. }
  33. add_action( 'wp_head', current_jquery( '1.4.2' ),0 );
  34. /*
  35. ==================================================================
  36. get the parent of the current page, or any page by id.
  37. ================================================================== */
  38. if(!function_exists('gs_get_page_parent'))
  39. {
  40. function gs_get_page_parent($ID = null,$param = null)
  41. {$idx = 0;
  42. if ($ID == null)
  43. {
  44. global $post;
  45. $ID = $post->ID;
  46. }
  47. do {
  48. $page = get_page($ID);
  49. $ID = $page->ID;
  50. if ($idx++ > 100) { print "recursion loop ".__file__." : ".__line__; break; }
  51. $return = (is_null($param)? $page->post_name : $page->$param);
  52. $ID = $page->post_parent;
  53. } while ($page->post_parent != 0);
  54. return $return;
  55. }
  56. }
  57. /*
  58. ==================================================================
  59. Save Options.
  60. ================================================================== */
  61. if (!function_exists('gs_save_options') )
  62. {
  63. function gs_save_options($config_group = null,$args)
  64. {
  65. if (!is_array($args)) { return; }
  66. foreach ($args as $option_name => $option_value)
  67. {
  68. if (!is_null($config_group)) { $option_name = $config_group.'-'.$option_name; }
  69. update_option( $option_name, $option_value );
  70. }
  71. }
  72. }
  73. if (!function_exists('gs_get_options') )
  74. {
  75. function gs_get_options($config_group = null)
  76. {
  77. $options = array();
  78. if (is_null($config_group) ) { return; }
  79. global $wpdb;
  80. $sql = "select * from " . $wpdb->prefix."options " . "where option_name like '%s'";
  81. $results = $wpdb->get_results($wpdb->prepare($sql,'%'.$config_group.'%'));
  82. if (!is_array($results)) { return; }
  83. foreach($results as $result)
  84. {
  85. $options[$result->option_name] = $result;
  86. }
  87. return $options;
  88. }
  89. }
  90. /*
  91. ==================================================================
  92. build selection box, from gs_base.
  93. ================================================================== */
  94. if (!function_exists ('gs_build_option_selection'))
  95. {
  96. function gs_build_option_selection($curr_value = null,$options)
  97. {
  98. // build and <option value = "1">test</option>
  99. // from an array.
  100. // [0] => array( [value] = 1, [display] => 'test')
  101. $option_html = '';
  102. if (!is_array($options)) { return false; }
  103. foreach ($options as $idx => $option)
  104. {
  105. if (is_array($option) ) { extract($option); }
  106. else
  107. {
  108. $value = $idx;
  109. $display = $option;
  110. }
  111. $option_html .= '<option value = "' . htmlentities($value) . '" ' . ($value == $curr_value? 'selected' : '') . '>' . htmlentities($display) . '</option>'."\n";
  112. }
  113. return $option_html;
  114. }
  115. }
  116. /*
  117. ==================================================================
  118. prevent reposts
  119. ================================================================== */
  120. if (!function_exists('gs_prevent_repost') )
  121. {
  122. function gs_prevent_repost($post_once)
  123. {
  124. if (strlen($post_once) < 1 ) return false; // no post once,
  125. if ($_SESSION['post_once'] == $post_once) { return true; } // this is a reload_so ignore it.
  126. $_SESSION['post_once'] = $post_once;
  127. return false; // not a reload.
  128. }
  129. }
  130. /*
  131. ==================================================================
  132. media selection dropdown.
  133. ================================================================== */
  134. if(!function_exists('gs_media_selector'))
  135. {
  136. function gs_wp_media_selector($curr_value = null,$field_name = null,$class_name = null)
  137. {
  138. $args = array(
  139. 'post_type' => 'attachment',
  140. 'numberposts' => -1,
  141. 'post_status' => null,
  142. 'post_parent' => null
  143. );
  144. $attachments = get_posts($args);
  145. $media_array = array();
  146. $media_array[] = array('value' => '', 'display' => 'Select an image from the WP Media Library');
  147. if ($attachments)
  148. {
  149. foreach($attachments as $attachment)
  150. {
  151. if (!preg_match("/image/",$attachment->post_mime_type) ) { continue; }
  152. $media_array[] = array('value' => $attachment->ID, 'display' => $attachment->post_title.' ');
  153. }
  154. }
  155. return '<select name = "' . $field_name . '" class = "' . $class_name . '">'.gs_build_option_selection( $curr_value,$media_array).'</select>';
  156. }
  157. }
  158. /*
  159. ==================================================================
  160. post selector
  161. ================================================================== */
  162. if(!function_exists('gs_wp_post_selector'))
  163. {
  164. function gs_wp_post_selector($curr_value = null,$field_name = null,$class_name = null)
  165. {
  166. $args = array(
  167. 'post_type' => 'post',
  168. 'orderby' => 'title',
  169. );
  170. $all_posts = get_posts($args);
  171. $post_array = array();
  172. $post_array[] = array('value' => '', 'display' => 'Select a WP post title');
  173. if ($all_posts)
  174. {
  175. foreach($all_posts as $post)
  176. {
  177. $post_array[] = array('value' => $post->ID, 'display' => $post->post_title.' ');
  178. }
  179. }
  180. return '<select name = "' . $field_name . '" class = "' . $class_name . '">'.gs_build_option_selection( $curr_value,$post_array).'</select>';
  181. }
  182. }
  183. /*
  184. ==================================================================
  185. page selector
  186. ================================================================== */
  187. if(!function_exists('gs_wp_page_selector'))
  188. {
  189. function gs_wp_page_selector($curr_value = null,$field_name = null,$class_name = null)
  190. {
  191. $post_array = array();
  192. $post_array[] = array('value' => '', 'display' => 'Select a WP Page by title');
  193. $all_posts = gs_wp_page_heirarchy(0);
  194. if ($all_posts)
  195. {
  196. foreach($all_posts as $post)
  197. {
  198. $post_array[] = array('value' => $post['ID'], 'display' => $post['title'].' ');
  199. }
  200. }
  201. return '<select name = "' . $field_name . '" class = "' . $class_name . '">'.gs_build_option_selection( $curr_value,$post_array).'</select>';
  202. }
  203. }
  204. /*
  205. ==================================================================
  206. Flatened page heirarchy
  207. ================================================================== */
  208. if(!function_exists('gs_wp_page_heirarchy'))
  209. {
  210. function gs_wp_page_heirarchy($ID = 0,$child_indicator = '-',$curr_indicator = '')
  211. {
  212. if (!is_numeric($ID)) { return; }
  213. $all_pages = array();
  214. $args = array(
  215. 'post_type' => 'page',
  216. 'orderby' => 'title',
  217. 'post_parent' => $ID
  218. );
  219. $all_posts = get_posts($args);
  220. if (!is_array($all_posts) || count($all_posts) < 1) { return; }
  221. foreach ($all_posts as $page_post)
  222. {
  223. $page_post->post_title = $curr_indicator.$page_post->post_title;
  224. $all_pages[] = array ('ID' => $page_post->ID, 'title' => $page_post->post_title);
  225. // check for children.
  226. $all_children = gs_wp_page_heirarchy($page_post->ID,$child_indicator,$child_indicator.$curr_indicator);
  227. if (!is_array($all_children) || count($all_children) < 1) { continue; }
  228. foreach($all_children as $child)
  229. {
  230. $all_pages[] = $child;
  231. }
  232. }
  233. return $all_pages;
  234. }
  235. }
  236. /*
  237. ==================================================================
  238. cat selector
  239. ================================================================== */
  240. if(!function_exists('gs_wp_category_selector'))
  241. {
  242. function gs_wp_category_selector($curr_value = null,$field_name = null,$class_name = null)
  243. {
  244. $post_array = array();
  245. $post_array[] = array('value' => '', 'display' => 'Select a Category');
  246. $all_posts = gs_wp_category_heirarchy(0);
  247. if ($all_posts)
  248. {
  249. foreach($all_posts as $post)
  250. {
  251. $post_array[] = array('value' => $post['ID'], 'display' => $post['cat_name'].' ');
  252. }
  253. }
  254. return '<select name = "' . $field_name . '" class = "' . $class_name . '">'.gs_build_option_selection( $curr_value,$post_array).'</select>';
  255. }
  256. }
  257. /*
  258. ==================================================================
  259. Flatened cat heirarchy
  260. ================================================================== */
  261. if(!function_exists('gs_wp_category_heirarchy'))
  262. {
  263. function gs_wp_category_heirarchy($ID = 0,$child_indicator = '-',$curr_indicator = '')
  264. {
  265. if (!is_numeric($ID)) { return; }
  266. $all_categories = array();
  267. $args = array(
  268. 'post_type' => 'post',
  269. 'orderby' => 'name',
  270. 'parent' => $ID,
  271. 'hide_empty' => false,
  272. 'hierarchical' => true
  273. );
  274. $all_cats = get_categories($args);
  275. if (!is_array($all_cats) || count($all_cats) < 1) { return; }
  276. foreach ($all_cats as $cat)
  277. {
  278. $cat->cat_name = $curr_indicator.$cat->cat_name;
  279. $all_categories[] = array ('ID' => $cat->cat_ID, 'cat_name' => $cat->cat_name);
  280. // check for children.
  281. $all_children = gs_wp_category_heirarchy($cat->cat_ID,$child_indicator,$child_indicator.$curr_indicator);
  282. if (!is_array($all_children) || count($all_children) < 1) { continue; }
  283. foreach($all_children as $child)
  284. {
  285. $all_categories[] = $child;
  286. }
  287. }
  288. return $all_categories;
  289. }
  290. }
  291. /*
  292. ==================================================================
  293. Build image, takes an argument, and determines if it
  294. is either a attatchment ID, or a URL.
  295. Formats the image to the correct size, returns the
  296. url and full html.
  297. size = From wp_get_attachment_image_src()
  298. (string|array) Size of the image shown for an image attachment:
  299. either a string keyword (thumbnail, medium, large or full)
  300. or a 2-item array representing width and height in pixels, e.g. array(32,32)
  301. as a bacground image.
  302. <div id="header" role="banner" style = "margin: 0; padding: 0; background-image: url(<?php gs_page_banner(null,'url') ?>);" >
  303. ================================================================== */
  304. if(!function_exists('gs_basic_get_wp_image'))
  305. {
  306. function gs_basic_get_wp_image($img_resource, $size = 'full', $class = '')
  307. {
  308. if (preg_match("/^\d+$/",$img_resource))
  309. {
  310. list($img_url, $w, $h) = wp_get_attachment_image_src( $img_resource,$size,true);
  311. }
  312. else
  313. {
  314. $img_url = $img_resource;
  315. }
  316. $img_html = '<img src = "'.$img_url.'" class = "'.$class.'" '.(is_numeric($w)? "width='$w'" : '').(is_numeric($h)? "height='$h'" : '').' />';
  317. $image->url = $img_url;
  318. $image->html = $img_html;
  319. $image->w = $w;
  320. $image->h = $h;
  321. return $image;
  322. }
  323. }
  324. /*
  325. ==================================================================
  326. function to print pre an array or object
  327. ================================================================== */
  328. if(!function_exists('_pre'))
  329. {
  330. function _pre($arg,$return = 0)
  331. {
  332. $rtn = "<pre>".print_r($arg,1)."</pre>";
  333. if ($return == 0) { print $rtn; return;}
  334. return $rtn;
  335. }
  336. }
  337. /*
  338. ==================================================================
  339. a function to validate a email in the correct format.
  340. ================================================================== */
  341. if (!function_exists('is_valid_email'))
  342. {
  343. function is_valid_email($email)
  344. {
  345. if( !preg_match( "/^[\w|\.|-]+@[\w|\.|-]+$/", $email)) {return false;}
  346. return true;
  347. }
  348. }
  349. /*
  350. ==================================================================
  351. start sessions.
  352. ================================================================== */
  353. if (!function_exists('gs_start_session') )
  354. {
  355. function gs_start_session()
  356. {
  357. if (strlen($_SERVER['HTTP_HOST']) < 1) { return;} // this is not a web reqeust, probably a cron request.
  358. if (!session_id()) { session_start();}
  359. }
  360. }
  361. if (!function_exists('gs_basic_flash_error'))
  362. {
  363. function gs_basic_flash_error($msg, $additional_class = '')
  364. {
  365. return '<div class = "error gs_basic_flash_message '. $additional_class .'" ><h3>Error:</h3><p>' . $msg . '</p></div>';
  366. }
  367. function gs_basic_flash_message($msg, $additional_class = '')
  368. {
  369. return '<div class = "message gs_basic_flash_message '. $additional_class .'" ><h3>Message:</h3><p>' . $msg . '</p></div>';
  370. }
  371. function gs_basic_flash_updated($msg, $additional_class = '')
  372. {
  373. return '<div class = "updated gs_basic_flash_message '. $additional_class .'" ><p>' . $msg . '</p></div>';
  374. }
  375. }
  376. /*
  377. ==================================================================
  378. Tiny MCE initer
  379. Usage.
  380. this must be some where in the head of the php.
  381. print gs_basic_que_tinymce();
  382. then call this
  383. gs_basic_tinymce('some_id'); after each text area.
  384. be sure to do this before you save the contents.
  385. tinyMCE.triggerSave();
  386. ================================================================== */
  387. if (!function_exists('gs_basic_tinymce'))
  388. {
  389. function gs_basic_que_tinymce()
  390. {
  391. return '<script type="text/javascript" src="'. WP_PLUGIN_URL .'/gs-basic-functions/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>';
  392. }
  393. function gs_basic_tinymce($element_id,$button_group = null)
  394. {
  395. $return = '
  396. <!-- TinyMCE -->
  397. <!-- // this is moved into a seperate function <script type="text/javascript" src="'. WP_PLUGIN_URL .'/gs-basic-functions/tinymce/jscripts/tiny_mce/tiny_mce.js"></script> -->
  398. <script type="text/javascript">
  399. tinyMCE.init({
  400. // General options
  401. mode : "exact",
  402. elements : "'.$element_id.'",
  403. theme : "advanced",
  404. plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",
  405. // Theme options
  406. ';
  407. switch ($button_group)
  408. {
  409. case 'advanced':
  410. {
  411. $return .= '
  412. theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
  413. theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
  414. theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,ltr,rtl,|,fullscreen",
  415. theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking",
  416. theme_advanced_toolbar_location : "top",
  417. theme_advanced_toolbar_align : "left",
  418. theme_advanced_statusbar_location : "bottom",
  419. theme_advanced_resizing : true,
  420. ';
  421. }break;
  422. default:
  423. {
  424. $return .= '
  425. theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
  426. theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
  427. theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
  428. theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
  429. theme_advanced_toolbar_location : "top",
  430. theme_advanced_toolbar_align : "left",
  431. theme_advanced_statusbar_location : "bottom",
  432. theme_advanced_resizing : true,
  433. ';
  434. }break;
  435. }
  436. // Example content CSS (should be your site CSS)
  437. //content_css : "css/content.css",
  438. // Drop lists for link/image/media/template dialogs
  439. //template_external_list_url : "lists/template_list.js",
  440. //external_link_list_url : "lists/link_list.js",
  441. //external_image_list_url : "lists/image_list.js",
  442. //media_external_list_url : "lists/media_list.js",
  443. // Style formats
  444. $return .= '
  445. style_formats : [
  446. {title : "Bold text", inline : "b"},
  447. {title : "Red text", inline : "span", styles : {color : "#ff0000"}},
  448. {title : "Red header", block : "h1", styles : {color : "#ff0000"}},
  449. {title : "Example 1", inline : "span", classes : "example1"},
  450. {title : "Example 2", inline : "span", classes : "example2"},
  451. {title : "Table styles"},
  452. {title : "Table row 1", selector : "tr", classes : "tablerow1"}
  453. ],
  454. // Replace values for the template plugin
  455. template_replace_values : {
  456. username : "Some User",
  457. staffid : "991234"
  458. }
  459. });
  460. </script>
  461. <!-- /TinyMCE -->';
  462. return $return;
  463. }
  464. }
  465. /*
  466. ==================================================================
  467. basic js and css
  468. ================================================================== */
  469. wp_enqueue_style( 'gs-basic-functions', WP_PLUGIN_URL.'/gs-basic-functions/gs-basic-functions.css');
  470. wp_enqueue_script('gs-basic-functions',WP_PLUGIN_URL.'/gs-basic-functions/gs-basic-functions.js', array('jquery'));
  471. wp_enqueue_script('jscolor',WP_PLUGIN_URL.'/gs-basic-functions/jscolor/jscolor.js');