PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-shopping-cart/widgets/category_widget.php

https://github.com/alx/barceloneta
PHP | 203 lines | 142 code | 39 blank | 22 comment | 34 complexity | ceb14e41d9eba5db67cf110b6d3058ac MD5 | raw file
  1. <?php
  2. function widget_wpsc_categorisation( $args, $widget_args = 1 ) {
  3. global $wpdb;
  4. extract( $args, EXTR_SKIP );
  5. if ( is_numeric($widget_args) )
  6. $widget_args = array( 'number' => $widget_args );
  7. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  8. extract( $widget_args, EXTR_SKIP );
  9. // Data should be stored as array: array( number => data for that instance of the widget, ... )
  10. $option_name = 'widget_wpsc_categorisation';
  11. $options = get_option($option_name);
  12. if ( !isset($options[$number]) )
  13. return;
  14. $my_options = $options[$number];
  15. $title = empty($my_options['title']) ? TXT_WPSC_CATEGORISATION : $my_options['title'];
  16. echo $before_widget;
  17. $full_title = $before_title . $title . $after_title;
  18. echo $full_title;
  19. $selected_categorisations = array_keys($my_options['categorisation'], true);
  20. if($selected_categorisations != null) {
  21. foreach($selected_categorisations as $key => $selected_categorisation) {
  22. $selected_categorisations[$key] = (int)$selected_categorisation;
  23. }
  24. $selected_values = implode(',',$selected_categorisations);
  25. $categorisation_groups = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}wpsc_categorisation_groups` WHERE `id` IN ({$selected_values}) AND `active` IN ('1')", ARRAY_A);
  26. foreach($categorisation_groups as $categorisation_group) {
  27. echo "<div id='categorisation_group_".$categorisation_group['id']."'>\n\r";
  28. if(count($categorisation_groups) > 1) { // no title unless multiple category groups
  29. echo "<h2 class='categorytitle'>{$categorisation_group['name']}</h2>\n\r";
  30. }
  31. show_cats_brands($categorisation_group['id'], 'sidebar', 'name', $my_options['image']);
  32. echo "\n\r";
  33. echo "</div>\n\r";
  34. }
  35. //echo("<pre>".print_r($selected_categorisations,true)."</pre>");
  36. } else {
  37. show_cats_brands(null, 'sidebar');
  38. }
  39. echo $after_widget;
  40. }
  41. // Displays form for a particular instance of the widget. Also updates the data after a POST submit
  42. // $widget_args: number
  43. // number: which of the several widgets of this type do we mean
  44. function widget_wpsc_categorisation_control( $widget_args = 1 ) {
  45. global $wp_registered_widgets, $wpdb;
  46. static $updated = false; // Whether or not we have already updated the data after a POST submit
  47. $option_name = 'widget_wpsc_categorisation';
  48. if ( is_numeric($widget_args) )
  49. $widget_args = array( 'number' => $widget_args );
  50. $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
  51. extract( $widget_args, EXTR_SKIP );
  52. // Data should be stored as array: array( number => data for that instance of the widget, ... )
  53. $options = get_option($option_name);
  54. if ( !is_array($options) )
  55. $options = array();
  56. // We need to update the data
  57. if ( !$updated && !empty($_POST['sidebar']) ) {
  58. // Tells us what sidebar to put the data in
  59. $sidebar = (string) $_POST['sidebar'];
  60. $sidebars_widgets = wp_get_sidebars_widgets();
  61. if ( isset($sidebars_widgets[$sidebar]) )
  62. $this_sidebar =& $sidebars_widgets[$sidebar];
  63. else
  64. $this_sidebar = array();
  65. foreach ( $this_sidebar as $_widget_id ) {
  66. // Remove all widgets of this type from the sidebar. We'll add the new data in a second. This makes sure we don't get any duplicate data
  67. // since widget ids aren't necessarily persistent across multiple updates
  68. if ( $option_name == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
  69. $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
  70. if ( !in_array( "categorisation-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "categorisation-$widget_number" is "{id_base}-{widget_number}
  71. unset($options[$widget_number]);
  72. }
  73. }
  74. foreach ( (array) $_POST[$option_name] as $widget_number => $widget_wpsc_categorisation_instance ) {
  75. // compile data from $widget_wpsc_categorisation_instance
  76. if ( !isset($widget_wpsc_categorisation_instance['title']) && isset($options[$widget_number]) ) // user clicked cancel
  77. continue;
  78. $options[$widget_number]['title'] = wp_specialchars($widget_wpsc_categorisation_instance['title']);
  79. $categorisation_groups = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}wpsc_categorisation_groups` WHERE `active` IN ('1')", ARRAY_A);
  80. foreach($categorisation_groups as $cat_group){
  81. if($widget_wpsc_categorisation_instance['categorisation'][$cat_group['id']] == "true") {
  82. $options[$widget_number]['categorisation'][$cat_group['id']] = true;
  83. } else {
  84. $options[$widget_number]['categorisation'][$cat_group['id']] = false;
  85. }
  86. }
  87. if ($widget_wpsc_categorisation_instance['image'] == "true") {
  88. $options[$widget_number]['image'] = true;
  89. } else {
  90. $options[$widget_number]['image'] = false;
  91. }
  92. }
  93. update_option($option_name, $options);
  94. $updated = true; // So that we don't go through this more than once
  95. }
  96. // Here we echo out the form
  97. if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
  98. $something = '';
  99. $number = '%i%';
  100. } else {
  101. $title = attribute_escape($options[$number]['title']);
  102. }
  103. //echo "<pre>".print_r($_POST,true)."</pre>";
  104. //echo "<pre>".print_r($options,true)."</pre>";
  105. echo "<p>\n\r";
  106. echo " <label for='{$option_name}-{$number}-title'>".__('Title:')."<input class='widefat' id='{$option_name}-{$number}-title' name='{$option_name}[{$number}][title]' type='text' value='{$title}' /></label>\n\r";
  107. echo " <input type='hidden' id='widget-categorisation-submit-$number' name='{$option_name}[$number][submit]' value='1' />\n\r";
  108. echo "</p>\n\r";
  109. echo "<p>\n\r";
  110. $categorisation_groups = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}wpsc_categorisation_groups` WHERE `active` IN ('1')", ARRAY_A);
  111. foreach($categorisation_groups as $cat_group){
  112. $checked = '';
  113. //$checked = "checked='true'";
  114. $category_count = $wpdb->get_var("SELECT COUNT(*) FROM `{$wpdb->prefix}product_categories` WHERE `group_id` IN ('{$cat_group['id']}')");
  115. $category_group_name = str_replace("[categorisation]", $cat_group['name'], TXT_WPSC_DISPLAY_PRODUCT_CATEGORIES);
  116. if($options[$number]['categorisation'][$cat_group['id']] == true) {
  117. $checked = "checked='true'";
  118. }
  119. if($category_count <1) {
  120. // if count of items is less than 1, disable it, but do it later, not a vital feture
  121. //$checked = "disabled='true' ";
  122. }
  123. $form_id = "{$option_name}-{$number}-group{$cat_group['id']}";
  124. echo " <label for='{$form_id}'>\n\r";
  125. echo " <input type='checkbox' name='{$option_name}[$number][categorisation][{$cat_group['id']}]' id='{$form_id}' value='true' class='checkbox' {$checked} />\n\r";
  126. echo " {$category_group_name}</label>\n\r";
  127. echo " <br/>\n\r";
  128. }
  129. if ($options[$number]['image'] == true) {
  130. $checked = "checked='true'";
  131. }
  132. echo " <label for='sidebar_category_image'>\n\r";
  133. echo " <input type='checkbox' name='{$option_name}[$number][image]' id='sidebar_category_image' value='true' class='checkbox' {$checked} />\n\r";
  134. echo " Display Images</label>\n\r";
  135. echo " <br/>\n\r";
  136. }
  137. // Registers each instance of our widget on startup
  138. function widget_wpsc_categorisation_register() {
  139. $option_name = 'widget_wpsc_categorisation';
  140. if ( !$options = get_option($option_name))
  141. $options = array();
  142. $widget_ops = array('classname' => 'widget_wpsc_categorisation', 'description' => __(TXT_WPSC_CATEGORISATION_DESCR));
  143. $control_ops = array('width' => 232, 'height' => 350, 'id_base' => 'wpsc_categorisation');
  144. $name = __(TXT_WPSC_CATEGORISATION);
  145. $registered = false;
  146. foreach ( array_keys($options) as $o ) {
  147. // Old widgets can have null values for some reason
  148. if ( !isset($options[$o]['title']) ) // we used 'something' above in our example. Replace with with whatever your real data are.
  149. continue;
  150. // $id should look like {$id_base}-{$o}
  151. $id = "wpsc_categorisation-$o"; // Never never never translate an id
  152. $registered = true;
  153. wp_register_sidebar_widget( $id, $name, 'widget_wpsc_categorisation', $widget_ops, array( 'number' => $o ) );
  154. wp_register_widget_control( $id, $name, 'widget_wpsc_categorisation_control', $control_ops, array( 'number' => $o ) );
  155. }
  156. // If there are none, we register the widget's existance with a generic template
  157. if ( !$registered ) {
  158. wp_register_sidebar_widget( 'wpsc_categorisation-1', $name, 'widget_wpsc_categorisation', $widget_ops, array( 'number' => -1 ) );
  159. wp_register_widget_control( 'wpsc_categorisation-1', $name, 'widget_wpsc_categorisation_control', $control_ops, array( 'number' => -1 ) );
  160. }
  161. }
  162. // This is important
  163. add_action( 'widgets_init', 'widget_wpsc_categorisation_register' );
  164. ?>