PageRenderTime 47ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/transfers-plugin/includes/widgets/widget-featured-services.php

https://gitlab.com/luyxtran264/myproject
PHP | 185 lines | 115 code | 41 blank | 29 comment | 21 complexity | 5e4570e1abb209e73eb9e284415daab7 MD5 | raw file
  1. <?php
  2. /*-----------------------------------------------------------------------------------
  3. Plugin Name: Transfers Featured Services Widget
  4. -----------------------------------------------------------------------------------*/
  5. // Add function to widgets_init that'll load our widget.
  6. add_action( 'widgets_init', 'transfers_featured_services_widgets' );
  7. // Register widget.
  8. function transfers_featured_services_widgets() {
  9. register_widget( 'transfers_Featured_Services_Widget' );
  10. }
  11. // Widget class.
  12. class transfers_Featured_Services_Widget extends WP_Widget {
  13. /*-----------------------------------------------------------------------------------*/
  14. /* Widget Setup
  15. /*-----------------------------------------------------------------------------------*/
  16. function __construct() {
  17. /* Widget settings. */
  18. $widget_ops = array( 'classname' => 'transfers_featured_services_widget', 'description' => esc_html__('Transfers: Featured Services', 'transfers') );
  19. /* Widget control settings. */
  20. $control_ops = array( 'width' => 260, 'height' => 400, 'id_base' => 'transfers_featured_services_widget' );
  21. /* Create the widget. */
  22. parent::__construct( 'transfers_featured_services_widget', esc_html__('Transfers: Featured Services', 'transfers'), $widget_ops, $control_ops );
  23. }
  24. /*-----------------------------------------------------------------------------------*/
  25. /* Display Widget
  26. /*-----------------------------------------------------------------------------------*/
  27. function widget( $args, $instance ) {
  28. global $transfers_plugin_globals, $transfers_services_post_type;
  29. $card_layout_classes = array(
  30. 'full-width',
  31. 'one-half',
  32. 'one-third',
  33. 'one-fourth',
  34. 'one-fifth'
  35. );
  36. extract( $args );
  37. /* Our variables from the widget settings. */
  38. $number_of_services = isset($instance['number_of_services']) ? (int)$instance['number_of_services'] : 4;
  39. $sort_by = isset($instance['sort_by']) ? $instance['sort_by'] : 'title';
  40. $sort_descending = isset($instance['sort_by']) && $instance['sort_descending'] == '1';
  41. $order = $sort_descending ? 'DESC' : 'ASC';
  42. $services_per_row = isset($instance['services_per_row']) ? (int)$instance['services_per_row'] : 4;
  43. global $display_mode;
  44. $display_mode = isset($instance['display_mode']) ? $instance['display_mode'] : 'card';
  45. $allowedtags = transfers_get_allowed_widgets_tags_array();
  46. echo wp_kses($before_widget, $allowedtags);
  47. if ($display_mode == 'card') {
  48. ?>
  49. <div class="services boxed white">
  50. <?php
  51. } else {
  52. ?>
  53. <div class="services boxed white small-list">
  54. <?php
  55. }
  56. /* Display Widget */
  57. $results = $transfers_services_post_type->list_services(0, $number_of_services, $sort_by, $order);
  58. if ( count($results) > 0 && $results['total'] > 0 ) {
  59. foreach ($results['results'] as $result) {
  60. global $post;
  61. $post = $result;
  62. setup_postdata( $post );
  63. global $post_class;
  64. if (isset($card_layout_classes[$services_per_row - 1]))
  65. $post_class = $card_layout_classes[$services_per_row - 1];
  66. else
  67. $post_class = 'one-fourth';
  68. get_template_part('includes/parts/service', 'item');
  69. }
  70. }
  71. ?></div><?php
  72. /* After widget (defined by themes). */
  73. echo wp_kses($after_widget, $allowedtags);
  74. // set back to default since this is a global variable
  75. $display_mode = 'card';
  76. }
  77. /*-----------------------------------------------------------------------------------*/
  78. /* Update Widget
  79. /*-----------------------------------------------------------------------------------*/
  80. function update( $new_instance, $old_instance ) {
  81. $instance = $old_instance;
  82. /* Strip tags to remove HTML (important for text inputs). */
  83. $instance['number_of_services'] = strip_tags( $new_instance['number_of_services']);
  84. $instance['sort_by'] = strip_tags( $new_instance['sort_by']);
  85. $instance['sort_descending'] = strip_tags( $new_instance['sort_descending']);
  86. $instance['display_mode'] = strip_tags( $new_instance['display_mode']);
  87. $instance['services_per_row'] = strip_tags( $new_instance['services_per_row']);
  88. return $instance;
  89. }
  90. /*-----------------------------------------------------------------------------------*/
  91. /* Widget Settings
  92. /*-----------------------------------------------------------------------------------*/
  93. function form( $instance ) {
  94. /* Set up some default widget settings. */
  95. $defaults = array(
  96. 'number_of_services' => '4',
  97. 'sort_by' => 'title',
  98. 'sort_descending' => '1',
  99. 'display_mode' => 'card',
  100. 'services_per_row' => 4
  101. );
  102. $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  103. <p>
  104. <label for="<?php echo esc_attr( $this->get_field_id( 'number_of_services' ) ); ?>"><?php esc_html_e('How many services do you want to display?', 'transfers') ?></label>
  105. <select id="<?php echo esc_attr( $this->get_field_id( 'number_of_services' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number_of_services' ) ); ?>">
  106. <?php for ($i=1;$i<13;$i++) { ?>
  107. <option <?php echo ($i == $instance['number_of_services'] ? 'selected="selected"' : ''); ?> value="<?php echo esc_attr ( $i ); ?>"><?php echo esc_html($i); ?></option>
  108. <?php } ?>
  109. </select>
  110. </p>
  111. <p>
  112. <label for="<?php echo esc_attr( $this->get_field_id( 'sort_by' ) ); ?>"><?php esc_html_e('What do you want to sort the services by?', 'transfers') ?></label>
  113. <select id="<?php echo esc_attr( $this->get_field_id( 'sort_by' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'sort_by') ); ?>">
  114. <option <?php echo 'title' == $instance['sort_by'] ? 'selected="selected"' : ''; ?> value="title"><?php esc_html_e('Post Title', 'transfers') ?></option>
  115. <option <?php echo 'ID' == $instance['sort_by'] ? 'selected="selected"' : ''; ?> value="ID"><?php esc_html_e('Post ID', 'transfers') ?></option>
  116. <option <?php echo 'rand' == $instance['sort_by'] ? 'selected="selected"' : ''; ?> value="rand"><?php esc_html_e('Random', 'transfers') ?></option>
  117. <option <?php echo 'date' == $instance['sort_by'] ? 'selected="selected"' : ''; ?> value="date"><?php esc_html_e('Publish Date', 'transfers') ?></option>
  118. <option <?php echo 'comment_count' == $instance['sort_by'] ? 'selected="selected"' : ''; ?> value="comment_count"><?php esc_html_e('Comment Count', 'transfers') ?></option>
  119. </select>
  120. </p>
  121. <p>
  122. <label for="<?php echo esc_attr( $this->get_field_id( 'sort_descending' ) ); ?>"><?php esc_html_e('Sort services in descending order?', 'transfers') ?></label>
  123. <input type="checkbox" <?php echo ($instance['sort_descending'] == '1' ? 'checked="checked"' : ''); ?> class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'sort_descending' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'sort_descending') ); ?>" value="1" />
  124. </p>
  125. <p>
  126. <label for="<?php echo esc_attr( $this->get_field_id( 'display_mode' ) ); ?>"><?php esc_html_e('Display mode?', 'transfers') ?></label>
  127. <select class="services_widget_display_mode" id="<?php echo esc_attr( $this->get_field_id( 'display_mode' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_mode') ); ?>">
  128. <option <?php echo 'small' == $instance['display_mode'] ? 'selected="selected"' : ''; ?> value="small"><?php esc_html_e('Small (usually sidebar)', 'transfers') ?></option>
  129. <option <?php echo 'card' == $instance['display_mode'] ? 'selected="selected"' : ''; ?> value="card"><?php esc_html_e('Card (usually in grid view)', 'transfers') ?></option>
  130. </select>
  131. </p>
  132. <p class="cards" <?php echo ( $instance['display_mode'] != 'card' ? 'style="display:none"' : '' ); ?>>
  133. <label for="<?php echo esc_attr ( $this->get_field_id( 'services_per_row' ) ); ?>"><?php esc_html_e('How many services do you want to display per row?', 'transfers') ?></label>
  134. <select id="<?php echo esc_attr ( $this->get_field_id( 'services_per_row' ) ); ?>" name="<?php echo esc_attr ( $this->get_field_name( 'services_per_row' ) ); ?>">
  135. <?php for ($i=1;$i<6;$i++) { ?>
  136. <option <?php echo ($i == $instance['services_per_row'] ? 'selected="selected"' : ''); ?> value="<?php echo esc_attr ( $i ); ?>"><?php echo esc_html($i); ?></option>
  137. <?php } ?>
  138. </select>
  139. </p>
  140. <?php
  141. }
  142. }