Advertisement
bowenac

checkbox limit

Jan 10th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Tokokoo Framework
  5. *
  6. * @category Tokokoo
  7. * @package Tokokoo Themes
  8. * @copyright Copyright (c) 2010-2011 Tokokoo
  9. * @license http://www.opensource.org/licenses/gpl-2.0.php GNU GPL version 2
  10. * @version $Id:$
  11. *
  12. * @developer Onnay Okheng (@onnayokheng)
  13. */
  14.  
  15. // Do not delete these lines
  16. if (!empty($_SERVER['SCRIPT_FILENAME']) && 'functions.php' == basename($_SERVER['SCRIPT_FILENAME']))
  17. die('Please do not load this page directly. Thanks!');
  18.  
  19. function wp_debug($variable, $die = false) {
  20. echo '<pre>' . print_r($variable, true) . '</pre>';
  21. if ($die)
  22. die();
  23. }
  24.  
  25. ################################################################################
  26. // Add theme panels
  27. ################################################################################
  28.  
  29. include 'resources/path.php';
  30. require_once ('admin/admin-functions.php');
  31. require_once ('admin/admin-interface.php');
  32. require_once ('admin/theme-options.php');
  33. require_once ('admin/theme-functions.php');
  34. include_once ('admin/theme-enqueue.php');
  35. include_once ('functions/themes-shortcode.php'); // include shortcode library.
  36. include_once ('resources/core.php');
  37.  
  38.  
  39.  
  40. ################################################################################
  41. // Add theme sidebars
  42. ################################################################################
  43.  
  44. if (function_exists('register_sidebar')) {
  45. register_sidebar(array(
  46. 'name' => __('Blog Sidebar'),
  47. 'description' => 'This widget appears on the blog sidebar',
  48. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  49. 'after_widget' => '</div>',
  50. 'before_title' => '<p class="widget-title">',
  51. 'after_title' => '</p>',
  52. ));
  53. register_sidebar(array(
  54. 'name' => __('Bottom Left'),
  55. 'description' => 'This widget appears on the bottom area above footer',
  56. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  57. 'after_widget' => '</div>',
  58. 'before_title' => '<p class="widget-title">',
  59. 'after_title' => '</p>',
  60. ));
  61. register_sidebar(array(
  62. 'name' => __('Bottom Center Left'),
  63. 'description' => 'This widget appears on the bottom area above footer',
  64. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  65. 'after_widget' => '</div>',
  66. 'before_title' => '<p class="widget-title">',
  67. 'after_title' => '</p>',
  68. ));
  69. register_sidebar(array(
  70. 'name' => __('Bottom Center Right'),
  71. 'description' => 'This widget appears on the bottom area above footer',
  72. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  73. 'after_widget' => '</div>',
  74. 'before_title' => '<p class="widget-title">',
  75. 'after_title' => '</p>',
  76. ));
  77. register_sidebar(array(
  78. 'name' => __('Bottom Right'),
  79. 'description' => 'This widget appears on the bottom area above footer',
  80. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  81. 'after_widget' => '</div>',
  82. 'before_title' => '<p class="widget-title">',
  83. 'after_title' => '</p>',
  84. ));
  85. register_sidebar(array(
  86. 'name' => __('Product Sidebar'),
  87. 'description' => 'This widget appears on the single product sidebar',
  88. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  89. 'after_widget' => '</div>',
  90. 'before_title' => '<p class="widget-title">',
  91. 'after_title' => '</p>',
  92. ));
  93. }
  94.  
  95. ################################################################################
  96. // Add theme support
  97. ################################################################################
  98.  
  99. if (function_exists('add_theme_support')) {
  100. add_theme_support('post-thumbnails');
  101. set_post_thumbnail_size(150, 150, true); // Normal post thumbnails
  102. // add_theme_support('post-formats', array('aside', 'gallery')); // Post Format support. You can also use the legacy "gallery" or "asides" (note the plural) categories.
  103. add_image_size('slider', 1250, 500, true); // slider
  104. add_image_size('blog', 462, 341, true); // blog
  105. add_image_size('best-big', 735, 570, true); // home best seller big
  106. add_image_size('best-landscape', 515, 300, true); // home best seller / sale landscape
  107. add_image_size('sale-portrait', 250, 570, true); // sale portrait
  108.  
  109. add_theme_support('automatic-feed-links'); // Add default posts and comments RSS feed links to head
  110. // add_custom_background(); // Add custom background functions
  111. add_theme_support('custom-background');; // Add custom background functions
  112. }
  113.  
  114. function show_posts_nav() {
  115. global $wp_query;
  116. return ($wp_query->max_num_pages > 1);
  117. }
  118.  
  119.  
  120.  
  121. ################################################################################
  122. // Configure WP2.9+ Navigation Menus
  123. ################################################################################
  124.  
  125. register_nav_menus(
  126. array(
  127. 'header' => __('Header Menu', 'tokokoo'),
  128. 'footer' => __('Footer Menu', 'tokokoo')
  129. ));
  130.  
  131.  
  132.  
  133. ################################################################################
  134. // Fixing the Read More in the Excerpts
  135. // This removes the annoying [?] to a Read More link
  136. ################################################################################
  137.  
  138. function tk_excerpt_more($more) {
  139. global $post;
  140. // edit here if you like
  141. // return '&nbsp; <a href="'. get_permalink($post->ID) . '" title="Read more '.get_the_title($post->ID).'">Read more &rarr;</a>';
  142. return '&nbsp; <a href="'. get_permalink($post->ID) . '" title="' . sprintf(__('Read more %s', 'tokokoo'), get_the_title($post->ID)) .'">'. __('Read more &rarr;', 'tokokoo') . '</a>';
  143. }
  144. add_filter('excerpt_more', 'tk_excerpt_more');
  145.  
  146.  
  147.  
  148. ################################################################################
  149. // Disable plugin update
  150. ################################################################################
  151.  
  152. add_filter('site_transient_update_plugins', 'tk_remove_update_plugin');
  153. function tk_remove_update_plugin($value) {
  154.  
  155. $removed = 'woocommerce/woocommerce.php';
  156. if (isset($value->checked[$removed])) {
  157. unset($value->checked[$removed]);
  158. }
  159.  
  160. return $value;
  161. }
  162.  
  163. add_filter('http_request_args', 'tk_prevent_plugin_update', 10, 2);
  164. function tk_prevent_plugin_update($r, $url) {
  165.  
  166. if (0 === strpos($url, 'http://api.wordpress.org/plugins/update-check/')) {
  167.  
  168. $removed = 'woocommerce/woocommerce.php';
  169. $plugins = unserialize($r['body']['plugins']);
  170. unset($plugins->plugins[$removed]);
  171. unset($plugins->active[array_search($removed, $plugins->active)]);
  172. $r['body']['plugins'] = serialize($plugins);
  173. }
  174.  
  175. return $r;
  176. }
  177.  
  178.  
  179.  
  180. ################################################################################
  181. // Actions + Filters
  182. ################################################################################
  183.  
  184. // Remove links to the extra feeds (e.g. category feeds)
  185. remove_action( 'wp_head', 'feed_links_extra', 3 );
  186. // Remove links to the general feeds (e.g. posts and comments)
  187. remove_action( 'wp_head', 'feed_links', 2 );
  188. // Remove link to the RSD service endpoint, EditURI link
  189. remove_action( 'wp_head', 'rsd_link' );
  190. // Remove link to the Windows Live Writer manifest file
  191. remove_action( 'wp_head', 'wlwmanifest_link' );
  192. // Remove index link
  193. remove_action( 'wp_head', 'index_rel_link' );
  194. // Remove prev link
  195. remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
  196. // Remove start link
  197. remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
  198. // Display relational links for adjacent posts
  199. remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 );
  200. // Remove XHTML generator showing WP version
  201. remove_action( 'wp_head', 'wp_generator' );
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. ?>
  209.  
  210.  
  211.  
  212.  
  213. <?php
  214.  
  215. /**
  216. * Limit How Many Checkboxes Can Be Checked
  217. * http://gravitywiz.com/2012/06/11/limiting-how-many-checkboxes-can-be-checked/
  218. */
  219.  
  220. class GFLimitCheckboxes {
  221.  
  222. public static $field_limits;
  223.  
  224. function __construct($form_id, $field_limits) {
  225.  
  226. $this->field_limits = $field_limits;
  227.  
  228. add_filter("gform_pre_render_$form_id", array(&$this, 'pre_render'));
  229. add_filter("gform_validation_$form_id", array(&$this, 'validate'));
  230.  
  231. }
  232.  
  233. function pre_render($form) {
  234.  
  235. $script = '';
  236. $output_script = false;
  237.  
  238. foreach($form['fields'] as $field) {
  239.  
  240. $field_id = $field['id'];
  241.  
  242. if( !array_key_exists($field['id'], $this->field_limits) // if field limits not provided for this field
  243. || RGFormsModel::get_input_type($field) != 'checkbox' // or if this field is not a checkbox
  244. || !isset($this->field_limits[$field_id]['max']) // or if 'max' is not set for this field
  245. )
  246. continue;
  247.  
  248. $output_script = true;
  249. $max = $this->field_limits[$field_id]['max'];
  250.  
  251. $script .= "jQuery(\"#field_{$form['id']}_{$field['id']} .gfield_checkbox input:checkbox\").checkboxLimit({$max});";
  252.  
  253. }
  254.  
  255. GFFormDisplay::add_init_script($form['id'], 'limit_checkboxes', GFFormDisplay::ON_PAGE_RENDER, $script);
  256.  
  257. if($output_script):
  258. ?>
  259.  
  260. <script type="text/javascript">
  261. jQuery(document).ready(function($) {
  262. $.fn.checkboxLimit = function(n) {
  263.  
  264. var checkboxes = this;
  265.  
  266. this.toggleDisable = function() {
  267.  
  268. // if we have reached or exceeded the limit, disable all other checkboxes
  269. if(this.filter(':checked').length >= n) {
  270. var unchecked = this.not(':checked');
  271. unchecked.prop('disabled', true);
  272. }
  273. // if we are below the limit, make sure all checkboxes are available
  274. else {
  275. this.prop('disabled', false);
  276. }
  277.  
  278. }
  279.  
  280. // when form is rendered, toggle disable
  281. checkboxes.bind('gform_post_render', checkboxes.toggleDisable());
  282.  
  283. // when checkbox is clicked, toggle disable
  284. checkboxes.click(function(event) {
  285.  
  286. checkboxes.toggleDisable();
  287.  
  288. // if we are equal to or below the limit, the field should be checked
  289. return checkboxes.filter(':checked').length <= n;
  290. });
  291.  
  292. }
  293. });
  294. </script>
  295.  
  296. <?php
  297. endif;
  298.  
  299. return $form;
  300. }
  301.  
  302. function validate($validation_result) {
  303.  
  304. $form = $validation_result['form'];
  305.  
  306. foreach($form['fields'] as &$field) {
  307.  
  308. if(!$this->should_field_be_validated($form, $field))
  309. continue;
  310.  
  311. $field_id = $field['id'];
  312. $min = isset($this->field_limits[$field_id]['min']) ? $this->field_limits[$field_id]['min'] : false;
  313. $max = isset($this->field_limits[$field_id]['max']) ? $this->field_limits[$field_id]['max'] : false;
  314. $count = 0;
  315.  
  316. foreach($_POST as $key => $value) {
  317. if(strpos($key, "input_{$field_id}_") !== false)
  318. $count++;
  319. }
  320.  
  321. if($count < $min) {
  322. $field['failed_validation'] = true;
  323. $field['validation_message'] = sprintf( _n('You must select at least %s item.', 'You must select at least %s items.', $min), $min );
  324. $validation_result['is_valid'] = false;
  325. }
  326. else if($count > $max) {
  327. $field['failed_validation'] = true;
  328. $field['validation_message'] = sprintf( _n('You may only select %s item.', 'You may only select %s items.', $max), $max );
  329. $validation_result['is_valid'] = false;
  330. }
  331.  
  332. }
  333.  
  334. $validation_result['form'] = $form;
  335.  
  336. return $validation_result;
  337. }
  338.  
  339.  
  340. function should_field_be_validated($form, $field) {
  341.  
  342. // if no limits provided for this field
  343. if( !array_key_exists($field['id'], $this->field_limits) )
  344. return false;
  345.  
  346. // or if this field is not a checkbox
  347. if( RGFormsModel::get_input_type($field) != 'checkbox' )
  348. return false;
  349.  
  350. // or if this field is hidden
  351. if( RGFormsModel::is_field_hidden($form, $field, array()) )
  352. return false;
  353.  
  354. return true;
  355. }
  356.  
  357. }
  358.  
  359. new GFLimitCheckboxes(5, array(
  360. 5 => array(
  361. 'min' => 2,
  362. 'max' => 3
  363. ),
  364. 6 => array(
  365. 'max' => 3
  366. )
  367. ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement