| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\widgets; |
| 4 |
|
| 5 |
use WP_Widget; |
| 6 |
|
| 7 |
class Forums extends WP_Widget { |
| 8 |
/** |
| 9 |
* @var array |
| 10 |
*/ |
| 11 |
private $default_instance; |
| 12 |
|
| 13 |
function __construct() { |
| 14 |
parent::__construct( 'wpforo_forums', 'wpForo Forums', [ 'description' => 'Forum tree.' ] ); |
| 15 |
$this->init_local_vars(); |
| 16 |
add_action( 'wp_ajax_wpforo_load_ajax_widget_Forums', [ $this, 'load_ajax_widget' ] ); |
| 17 |
add_action( 'wp_ajax_nopriv_wpforo_load_ajax_widget_Forums', [ $this, 'load_ajax_widget' ] ); |
| 18 |
} |
| 19 |
|
| 20 |
private function init_local_vars() { |
| 21 |
$this->default_instance = [ |
| 22 |
'boardid' => 0, |
| 23 |
'title' => __( 'Forums', 'wpforo' ), |
| 24 |
'dropdown' => false, |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
public function get_widget( $instance ) { |
| 29 |
ob_start(); |
| 30 |
|
| 31 |
if ( wpfval( $instance, 'dropdown' ) ) { |
| 32 |
$forum_urls = []; |
| 33 |
$forums = array_filter( WPF()->forum->get_forums(), function ( $forum ) { |
| 34 |
return WPF()->perm->forum_can( 'vf', $forum['forumid'] ); |
| 35 |
} ); |
| 36 |
if ( ! empty( $forums ) ) { |
| 37 |
foreach ( $forums as $forum ) { |
| 38 |
$forum_urls[ 'forum_' . $forum['forumid'] ] = wpforo_home_url( $forum['slug'] ); |
| 39 |
} |
| 40 |
} |
| 41 |
if ( ! empty( $forum_urls ) ) { |
| 42 |
echo '<select onchange="window.location.href = wpf_forum_urls[\'forum_\' + this.value]">'; |
| 43 |
WPF()->forum->tree( 'select_box', true, WPF()->current_object['forumid'] ); |
| 44 |
echo '</select>'; |
| 45 |
?> |
| 46 |
<script> |
| 47 |
var wpf_forum_json = '<?php echo wp_json_encode( $forum_urls ) ?>'; |
| 48 |
var wpf_forum_urls = JSON.parse(wpf_forum_json); |
| 49 |
</script> |
| 50 |
<?php |
| 51 |
} |
| 52 |
} else { |
| 53 |
WPF()->forum->tree( 'front_list', true, WPF()->current_object['forumid'], false ); |
| 54 |
} |
| 55 |
|
| 56 |
return ob_get_clean(); |
| 57 |
} |
| 58 |
|
| 59 |
public function load_ajax_widget() { |
| 60 |
$_POST = wp_unslash( $_POST ); |
| 61 |
$instance = json_decode( (string) wpfval( $_POST, 'instance' ), true ); |
| 62 |
wp_send_json_success( [ 'html' => $this->get_widget( $instance ) ] ); |
| 63 |
} |
| 64 |
|
| 65 |
public function widget( $args, $instance ) { |
| 66 |
wp_enqueue_script( 'wpforo-widgets-js' ); |
| 67 |
$instance = wpforo_parse_args( $instance, $this->default_instance ); |
| 68 |
$data = [ |
| 69 |
'boardid' => $instance['boardid'], |
| 70 |
'action' => 'wpforo_load_ajax_widget_Forums', |
| 71 |
'instance' => $instance, |
| 72 |
]; |
| 73 |
if( WPF()->board->get_current( 'boardid' ) === $instance['boardid'] ){ |
| 74 |
$html = $this->get_widget( $data['instance'] ); |
| 75 |
$onload = false; |
| 76 |
}else{ |
| 77 |
$html = '<div style="text-align: center; font-size: 20px;"><i class="fas fa-spinner fa-spin"></i></div>'; |
| 78 |
$onload = true; |
| 79 |
$data['referer'] = home_url(); |
| 80 |
} |
| 81 |
$json = wp_json_encode( $data ); |
| 82 |
echo $args['before_widget'] . '<div id="wpf-widget-forums" class="wpforo-widget-wrap">'; |
| 83 |
if ( $instance['title'] ) echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
| 84 |
echo '<div class="wpforo-widget-content wpforo-ajax-widget ' . ( ! $onload ? 'wpforo-ajax-widget-onload-false' : '' ) . '" data-json="' . esc_attr( $json ) . '">' . $html . '</div></div>' . $args['after_widget']; |
| 85 |
} |
| 86 |
|
| 87 |
public function form( $instance ) { |
| 88 |
$instance = wpforo_parse_args( $instance, $this->default_instance ); |
| 89 |
$boardid = (int) $instance['boardid']; |
| 90 |
$title = (string) $instance['title']; |
| 91 |
$dropdown = (bool) $instance['dropdown']; |
| 92 |
WPF()->change_board( $boardid ); |
| 93 |
?> |
| 94 |
<div class="wpf-wdg-wrapper"> |
| 95 |
<p> |
| 96 |
<label><?php _e( 'Title', 'wpforo' ); ?>:</label> |
| 97 |
<label> |
| 98 |
<input class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> |
| 99 |
</label> |
| 100 |
</p> |
| 101 |
<p> |
| 102 |
<label for="<?php echo $this->get_field_id( 'boardid' ) ?>"></label> |
| 103 |
<select id="<?php echo $this->get_field_id( 'boardid' ) ?>" name="<?php echo esc_attr( $this->get_field_name( 'boardid' ) ); ?>"> |
| 104 |
<?php echo WPF()->board->dropdown( $boardid ) ?> |
| 105 |
</select> |
| 106 |
</p> |
| 107 |
<p> |
| 108 |
<span><?php _e( 'Display as dropdown', 'wpforo' ); ?> : </span> |
| 109 |
|
| 110 |
<label for="<?php echo $this->get_field_id( 'dropdown' ) ?>_1"><?php _e( 'Yes', 'wpforo' ); ?></label> |
| 111 |
<input id="<?php echo $this->get_field_id( 'dropdown' ) ?>_1" value="1" <?php checked( $dropdown ); ?> type="radio" name="<?php echo esc_attr( $this->get_field_name( 'dropdown' ) ); ?>"> |
| 112 |
|
| 113 |
<label for="<?php echo $this->get_field_id( 'dropdown' ) ?>_0"><?php _e( 'No', 'wpforo' ); ?></label> |
| 114 |
<input id="<?php echo $this->get_field_id( 'dropdown' ) ?>_0" value="0" <?php checked( $dropdown, false ); ?> type="radio" name="<?php echo esc_attr( $this->get_field_name( 'dropdown' ) ); ?>"> |
| 115 |
</p> |
| 116 |
</div> |
| 117 |
<?php |
| 118 |
} |
| 119 |
|
| 120 |
public function update( $new_instance, $old_instance ) { |
| 121 |
$new_instance = wpforo_parse_args( $new_instance, $this->default_instance ); |
| 122 |
$instance = []; |
| 123 |
$instance['boardid'] = (int) $new_instance['boardid']; |
| 124 |
$instance['title'] = strip_tags( (string) $new_instance['title'] ); |
| 125 |
$instance['dropdown'] = (bool) (int) $new_instance['dropdown']; |
| 126 |
|
| 127 |
return $instance; |
| 128 |
} |
| 129 |
} |
| 130 |
|