PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/site-categories/lib/widgets/class_site_categories_widget_categories.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 314 lines | 236 code | 66 blank | 12 comment | 77 complexity | 334af9fd863878e83be0b9e92840ac7b MD5 | raw file
  1. <?php
  2. /**
  3. * Shows all Blog categories.
  4. */
  5. class Bcat_WidgetCategories extends WP_Widget {
  6. function Bcat_WidgetCategories () {
  7. $widget_ops = array('classname' => __CLASS__, 'description' => __('Shows a list of all site categories.', SITE_CATEGORIES_I18N_DOMAIN));
  8. parent::WP_Widget(__CLASS__, __('Site Categories', SITE_CATEGORIES_I18N_DOMAIN), $widget_ops);
  9. }
  10. function form($instance) {
  11. // Set defaults
  12. // ...
  13. $defaults = array(
  14. 'title' => '',
  15. 'per_page' => 5,
  16. 'ordering' => 'name',
  17. 'order' => 'ASC',
  18. 'show_style' => 'ul',
  19. 'hide_empty' => 0,
  20. 'show_counts' => 1,
  21. 'icon_show' => 1,
  22. 'icon_size' => 32,
  23. 'show_more_link' => 1,
  24. 'landing_link_label' => __('more categories', SITE_CATEGORIES_I18N_DOMAIN)
  25. );
  26. $instance = wp_parse_args( (array) $instance, $defaults );
  27. //echo "instance<pre>"; print_r($instance); echo "</pre>";
  28. ?>
  29. <p>
  30. <label for="<?php echo $this->get_field_id('title') ?>"><?php _e('Title:', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  31. <input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>"
  32. class="widefat" value="<?php echo $instance['title'] ?> "/>
  33. </p>
  34. <p>
  35. <label for="<?php echo $this->get_field_id('show_style') ?>"><?php _e('Display as:', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  36. <select id="<?php echo $this->get_field_id( 'show_style' ); ?>"
  37. name="<?php echo $this->get_field_name( 'show_style'); ?>" class="widefat" style="width:100%;">
  38. <option value="ol" <?php if ($instance['show_style'] == "ol") { echo ' selected="selected" '; } ?>><?php
  39. _e('Ordered List (ol)', SITE_CATEGORIES_I18N_DOMAIN); ?></option>
  40. <option value="ul" <?php if ($instance['show_style'] == "ul") { echo ' selected="selected" '; } ?>><?php
  41. _e('Unordered List (ul)', SITE_CATEGORIES_I18N_DOMAIN); ?></option>
  42. <?php /* >
  43. <option value="select" <?php if ($instance['show_style'] == "select") { echo ' selected="selected" '; } ?>><?php
  44. _e('Dropdown (select)', SITE_CATEGORIES_I18N_DOMAIN); ?></option>
  45. <?php */ ?>
  46. </select>
  47. </p>
  48. <p>
  49. <label for="<?php echo $this->get_field_id( 'per_page' ); ?>"><?php
  50. _e('Number of Site Categories to show:', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  51. <input type="text" id="<?php echo $this->get_field_id( 'per_page' ); ?>" value="<?php echo intval($instance['per_page']); ?>"
  52. name="<?php echo $this->get_field_name( 'per_page'); ?>" class="widefat" style="width:100%;" />
  53. </p>
  54. <p>
  55. <label for="<?php echo $this->get_field_id('ordering') ?>"><?php _e('Ordering:', SITE_CATEGORIES_I18N_DOMAIN); ?></label><br />
  56. <select id="<?php echo $this->get_field_id( 'ordering' ); ?>"
  57. name="<?php echo $this->get_field_name( 'ordering'); ?>" class="widefat" style="width:60%;">
  58. <option value="name" <?php if ($instance['ordering'] == "name") { echo ' selected="selected" '; }?>><?php
  59. _e('Name', SITE_CATEGORIES_I18N_DOMAIN); ?></option>
  60. <option value="id" <?php if ($instance['ordering'] == "id") { echo ' selected="selected" '; }?>><?php
  61. _e('Category ID', SITE_CATEGORIES_I18N_DOMAIN); ?></option>
  62. </select>
  63. <select id="<?php echo $this->get_field_id( 'order' ); ?>"
  64. name="<?php echo $this->get_field_name( 'order'); ?>" class="widefat" style="width:25%;">
  65. <option value="ASC" <?php if ($instance['order'] == "ASC") { echo ' selected="selected" '; }?>><?php
  66. _e('ASC', SITE_CATEGORIES_I18N_DOMAIN); ?></option>
  67. <option value="DESC" <?php if ($instance['order'] == "DESC") { echo ' selected="selected" '; }?>><?php
  68. _e('DESC', SITE_CATEGORIES_I18N_DOMAIN); ?></option>
  69. </select>
  70. </p>
  71. <p>
  72. <label for="<?php echo $this->get_field_id('hide_empty') ?>"><?php _e('Hide Empty Site Categories:', SITE_CATEGORIES_I18N_DOMAIN); ?></label><br />
  73. <input type="radio" name="<?php echo $this->get_field_name( 'hide_empty'); ?>" id="<?php echo $this->get_field_id('hide_empty') ?>_yes"
  74. value="1" <?php if ($instance['hide_empty'] == "1") { echo ' checked="checked" '; } ?> /> <label for="<?php echo $this->get_field_id('hide_empty') ?>_yes"><?php _e('Yes', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  75. <input type="radio" name="<?php echo $this->get_field_name( 'hide_empty'); ?>" id="<?php echo $this->get_field_id('hide_empty') ?>_no"
  76. value="0" <?php if ($instance['hide_empty'] == "0") { echo ' checked="checked" '; } ?> /> <label for="<?php echo $this->get_field_id('hide_empty') ?>_no"><?php _e('No', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  77. </p>
  78. <p>
  79. <label for="<?php echo $this->get_field_id('show_counts') ?>"><?php _e('Show Site Category Count:', SITE_CATEGORIES_I18N_DOMAIN); ?></label><br />
  80. <input type="radio" name="<?php echo $this->get_field_name( 'show_counts'); ?>" id="<?php echo $this->get_field_id('show_counts') ?>_yes"
  81. value="1" <?php if ($instance['show_counts'] == "1") { echo ' checked="checked" '; } ?> /> <label for="<?php echo $this->get_field_id('show_counts') ?>_yes"><?php _e('Yes', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  82. <input type="radio" name="<?php echo $this->get_field_name( 'show_counts'); ?>" id="<?php echo $this->get_field_id('show_counts') ?>_no"
  83. value="0" <?php if ($instance['show_counts'] == "0") { echo ' checked="checked" '; } ?> /> <label for="<?php echo $this->get_field_id('show_counts') ?>_no"><?php _e('No', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  84. </p>
  85. <p>
  86. <label for="<?php echo $this->get_field_id('icon_show') ?>"><?php _e('Show Site Category icons:', SITE_CATEGORIES_I18N_DOMAIN); ?></label><br />
  87. <input type="radio" name="<?php echo $this->get_field_name( 'icon_show'); ?>" id="<?php echo $this->get_field_id('icon_show') ?>_yes"
  88. value="1" <?php if ($instance['icon_show'] == "1") { echo ' checked="checked" '; } ?> /> <label for="<?php echo $this->get_field_id('icon_show') ?>_yes"><?php _e('Yes', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  89. <input type="radio" name="<?php echo $this->get_field_name( 'icon_show'); ?>" id="<?php echo $this->get_field_id('icon_show') ?>_no"
  90. value="0" <?php if ($instance['icon_show'] == "0") { echo ' checked="checked" '; } ?> /> <label for="<?php echo $this->get_field_id('icon_show') ?>_no"><?php _e('No', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  91. </p>
  92. <p>
  93. <label for="<?php echo $this->get_field_id('icon_size') ?>"><?php _e('Icon size:', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  94. <input type="text" name="<?php echo $this->get_field_name('icon_size'); ?>" id="<?php echo $this->get_field_id('icon_size'); ?>"
  95. class="" size="5" value="<?php echo $instance['icon_size'] ?>"/>px <?php _e('square', SITE_CATEGORIES_I18N_DOMAIN); ?>
  96. </p>
  97. <p>
  98. <label for="<?php echo $this->get_field_id('show_more_link') ?>"><?php _e('Landing Page link below categories list:', SITE_CATEGORIES_I18N_DOMAIN); ?></label><br />
  99. <input type="radio" name="<?php echo $this->get_field_name( 'show_more_link'); ?>" id="<?php echo $this->get_field_id('show_more_link') ?>_yes"
  100. value="1" <?php if ($instance['show_more_link'] == "1") { echo ' checked="checked" '; } ?> /> <label for="<?php echo $this->get_field_id('show_more_link') ?>_yes"><?php _e('Yes', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  101. <input type="radio" name="<?php echo $this->get_field_name( 'show_more_link'); ?>" id="<?php echo $this->get_field_id('show_more_link') ?>_no"
  102. value="0" <?php if ($instance['show_more_link'] == "0") { echo ' checked="checked" '; } ?> /> <label for="<?php echo $this->get_field_id('show_more_link') ?>_no"><?php _e('No', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  103. </p>
  104. <p>
  105. <label for="<?php echo $this->get_field_id( 'landing_link_label' ); ?>"><?php
  106. _e('Label for link:', SITE_CATEGORIES_I18N_DOMAIN); ?></label>
  107. <input type="text" id="<?php echo $this->get_field_id( 'landing_link_label' ); ?>" value="<?php echo $instance['landing_link_label']; ?>"
  108. name="<?php echo $this->get_field_name( 'landing_link_label'); ?>" class="widefat" style="width:100%;" />
  109. </p>
  110. <?php
  111. }
  112. function update($new_instance, $old_instance) {
  113. $instance = $old_instance;
  114. $instance['title'] = strip_tags($new_instance['title']);
  115. $instance['per_page'] = strip_tags($new_instance['per_page']);
  116. $instance['ordering'] = strip_tags($new_instance['ordering']);
  117. $instance['order'] = strip_tags($new_instance['order']);
  118. $instance['hide_empty'] = intval($new_instance['hide_empty']);
  119. $instance['show_counts'] = intval($new_instance['show_counts']);
  120. $instance['show_style'] = strip_tags($new_instance['show_style']);
  121. $instance['icon_show'] = strip_tags($new_instance['icon_show']);
  122. $instance['icon_size'] = intval($new_instance['icon_size']);
  123. $instance['show_more_link'] = intval($new_instance['show_more_link']);
  124. $instance['landing_link_label'] = strip_tags($new_instance['landing_link_label']);
  125. delete_site_transient( 'site-categories-categories-data-'. $this->number );
  126. return $instance;
  127. }
  128. function widget($args, $instance) {
  129. global $site_categories, $current_site;
  130. $site_categories->load_config();
  131. extract($args);
  132. $per_page = intval($instance['per_page']);
  133. if (!$per_page) {
  134. if (isset($site_categories->opts['blog_limit']))
  135. $per_page = intval($site_categories->opts['blog_limit']);
  136. else
  137. $per_page = 10;
  138. }
  139. $instance['per_page'] = intval($per_page);
  140. $ordering = strip_tags($instance['ordering']);
  141. if (!$ordering) {
  142. if (isset($site_categories->opts['blog_ordering']))
  143. $ordering = @$default_opts['blog_ordering'];
  144. else
  145. $ordering = "alphabetical";
  146. }
  147. $instance['ordering'] = $ordering;
  148. $icon_size = intval($instance['icon_size']);
  149. if ((!$icon_size) || ($icon_size < 1)) {
  150. $icon_size = 32;
  151. }
  152. $instance['icon_size'] = $icon_size;
  153. $show_style = strip_tags($instance['show_style']);
  154. if (!$show_style) {
  155. $show_style = "ul";
  156. }
  157. $instance['show_style'] = $show_style;
  158. //echo "instance<pre>"; print_r($instance); echo "</pre>";
  159. $data = get_site_transient( 'site-categories-categories-data-'. $this->number );
  160. if (!$data) {
  161. switch_to_blog( $current_site->blog_id );
  162. $get_terms_args = array();
  163. $get_terms_args['hide_empty'] = $instance['hide_empty'];
  164. $get_terms_args['orderby'] = $instance['ordering'];
  165. $categories = get_terms( SITE_CATEGORIES_TAXONOMY, $get_terms_args );
  166. //echo "categories<pre>"; print_r($categories); echo "</pre>";
  167. if (($categories) && (count($categories))) {
  168. $data = array();
  169. $data['current_page'] = 1;
  170. if (count($categories) < $instance['per_page']) {
  171. $data['categories'] = $categories;
  172. } else {
  173. $data['offset'] = intval($instance['per_page']) * (intval($data['current_page'])-1);
  174. $data['categories'] = array_slice($categories, $data['offset'], $instance['per_page'], true);
  175. $data['total_pages'] = ceil(count($categories)/intval($instance['per_page']));
  176. }
  177. if (count($data['categories'])) {
  178. foreach($data['categories'] as $idx => $data_category) {
  179. $data['categories'][$idx]->icon_image_src = $site_categories->get_category_term_icon_src($data_category->term_id,
  180. $instance['icon_size']);
  181. if ((isset($site_categories->opts['landing_page_rewrite'])) && ($site_categories->opts['landing_page_rewrite'] == true)) {
  182. $data['categories'][$idx]->bcat_url = trailingslashit($site_categories->opts['landing_page_slug']) . $data_category->slug;
  183. } else {
  184. $data['categories'][$idx]->bcat_url = $site_categories->opts['landing_page_slug'] .'&amp;category_name=' . $data_category->slug;
  185. }
  186. }
  187. }
  188. if (intval($instance['show_more_link'])) {
  189. if ((isset($site_categories->opts['landing_page_rewrite'])) && ($site_categories->opts['landing_page_rewrite'] == true)) {
  190. $data['landing']['link_url'] = trailingslashit($site_categories->opts['landing_page_slug']);
  191. } else {
  192. $data['landing']['link_url'] = $site_categories->opts['landing_page_slug'];
  193. }
  194. if (isset($instance['landing_link_label']))
  195. $data['landing']['link_label'] = $instance['landing_link_label'];
  196. else
  197. $data['landing']['link_label'] = __('More Categories', SITE_CATEGORIES_I18N_DOMAIN);
  198. }
  199. }
  200. restore_current_blog();
  201. set_site_transient( 'site-categories-categories-data-'. $this->number, $data, 30);
  202. }
  203. $categories_content = apply_filters('categories_widget_list_display', '', $data, $instance);
  204. if (strlen($categories_content)) {
  205. echo $before_widget;
  206. $title = apply_filters('widget_title', $instance['title']);
  207. if ($title) echo $before_title . $title . $after_title;
  208. echo $categories_content;
  209. echo $after_widget;
  210. }
  211. }
  212. }
  213. function process_categories_widget_list_display($content, $data, $args) {
  214. //echo "args<pre>"; print_r($args); echo "</pre>";
  215. //echo "data<pre>"; print_r($data); echo "</pre>";
  216. if ((isset($data['categories'])) && (count($data['categories']))) {
  217. if ($args['show_style'] == "ol") { $content .= '<ol class="site-categories site-categories-widget">'; }
  218. else if ($args['show_style'] == "select") { $content .= '<select class="site-categories site-categories-widget">'; }
  219. else { $content .= '<ul class="site-categories site-categories-widget">'; }
  220. foreach ($data['categories'] as $category) {
  221. if ($args['show_style'] != "select") {
  222. $content .= '<li><a href="'. $category->bcat_url .'">';
  223. if ( ($args['icon_show'] == true) && (isset($category->icon_image_src))) {
  224. $content .= '<img class="site-category-icon" width="'. $args['icon_size'] .'" height="'. $args['icon_size'] .'"
  225. alt="'. $category->name .'" src="'. $category->icon_image_src .'" />';
  226. }
  227. $content .= '<span class="site-category-title">'. $category->name .'</span>';
  228. if ($args['show_counts']) {
  229. $content .= '<span class="site-category-count">('. $category->count .')</span>';
  230. }
  231. $content .= '</a>';
  232. $content .= '</li>';
  233. } else {
  234. $content .= '<option value="'. $category->bcat_url .'">'. $category->name .'</option>';
  235. }
  236. }
  237. if ($args['show_style'] == "ol") { $content .= "</ol>"; }
  238. else if ($args['show_style'] == "select") { $content .= "</select>"; }
  239. else { $content .= "</ul>"; }
  240. if ((isset($args['show_more_link'])) && ($args['show_more_link']) && (isset($data['landing']))) {
  241. $content .= '<div id="site-categories-navigation">';
  242. $content .= '<a href="'. $data['landing']['link_url'] .'">'. $data['landing']['link_label'] .'</a>';
  243. $content .= '</div>';
  244. }
  245. }
  246. return $content;
  247. }
  248. add_filter('categories_widget_list_display', 'process_categories_widget_list_display', 99, 3);