Completed
Push — master ( 2fd399...b41af7 )
by
unknown
08:39
created

BP_Nouveau_Members::setup_actions()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 9
Ratio 34.62 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 9
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * BP Nouveau Members
4
 *
5
 * @since 1.0.0
6
 */
7
8
// Exit if accessed directly.
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Members Loader class
13
 *
14
 * @since 1.0.0
15
 */
16
class BP_Nouveau_Members {
17
	/**
18
	 * Constructor
19
	 *
20
	 * @since 1.0.0
21
	 */
22
	public function __construct() {
23
		$this->setup_globals();
24
		$this->includes();
25
		$this->setup_actions();
26
		$this->setup_filters();
27
	}
28
29
	/**
30
	 * Globals
31
	 *
32
	 * @since 1.0.0
33
	 */
34
	protected function setup_globals() {
35
		$this->dir                  = dirname( __FILE__ );
36
		$this->is_user_home_sidebar = false;
37
	}
38
39
	/**
40
	 * Include needed files
41
	 *
42
	 * @since 1.0.0
43
	 */
44
	protected function includes() {
45
		require( trailingslashit( $this->dir ) . 'functions.php' );
46
		require( trailingslashit( $this->dir ) . 'template-tags.php' );
47
	}
48
49
	/**
50
	 * Register do_action() hooks
51
	 *
52
	 * @since 1.0.0
53
	 */
54
	protected function setup_actions() {
55
		$ajax_actions = array(
56
			array(
57
				'members_filter' => array(
58
					'function' => 'bp_nouveau_ajax_object_template_loader',
59
					'nopriv'   => true,
60
				),
61
			),
62
		);
63
64 View Code Duplication
		foreach ( $ajax_actions as $ajax_action ) {
65
			$action = key( $ajax_action );
66
67
			add_action( 'wp_ajax_' . $action, $ajax_action[ $action ]['function'] );
68
69
			if ( ! empty( $ajax_action[ $action ]['nopriv'] ) ) {
70
				add_action( 'wp_ajax_nopriv_' . $action, $ajax_action[ $action ]['function'] );
71
			}
72
		}
73
74
		add_action( 'bp_nouveau_enqueue_scripts', 'bp_nouveau_members_enqueue_scripts' );
75
76
		// Actions to check whether we are in the member's default front page sidebar
77
		add_action( 'dynamic_sidebar_before', array( $this, 'user_home_sidebar_set' ), 10, 1 );
78
		add_action( 'dynamic_sidebar_after', array( $this, 'user_home_sidebar_unset' ), 10, 1 );
79
	}
80
81
	/**
82
	 * Register add_filter() hooks
83
	 *
84
	 * @since 1.0.0
85
	 */
86
	protected function setup_filters() {
87
		// Add the default-front to User's front hierarchy if user enabled it (Enabled by default).
88
		add_filter( 'bp_displayed_user_get_front_template', 'bp_nouveau_member_reset_front_template', 10, 1 );
89
	}
90
91
	/**
92
	 * Add filters to be sure the (BuddyPress) widgets display will be consistent
93
	 * with the displayed user's default front page.
94
	 *
95
	 * @since 1.0.0
96
	 *
97
	 * @param string $sidebar_index The Sidebar identifier.
98
	 */
99
	public function user_home_sidebar_set( $sidebar_index = '' ) {
100
		if ( 'sidebar-buddypress-members' !== $sidebar_index ) {
101
			return;
102
		}
103
104
		$this->is_user_home_sidebar = true;
105
106
		// Add needed filters.
107
		bp_nouveau_members_add_home_widget_filters();
108
	}
109
110
	/**
111
	 * Remove filters to be sure the (BuddyPress) widgets display will no more take
112
	 * the displayed user in account.
113
	 *
114
	 * @since 1.0.0
115
	 *
116
	 * @param  string $sidebar_index The Sidebar identifier.
117
	 */
118
	public function user_home_sidebar_unset( $sidebar_index = '' ) {
119
		if ( 'sidebar-buddypress-members' !== $sidebar_index ) {
120
			return;
121
		}
122
123
		$this->is_user_home_sidebar = false;
124
125
		// Remove no more needed filters.
126
		bp_nouveau_members_remove_home_widget_filters();
127
	}
128
}
129
130
/**
131
 * Launch the Members loader class.
132
 *
133
 * @since 1.0.0
134
 */
135
function bp_nouveau_members( $bp_nouveau = null ) {
136
	if ( is_null( $bp_nouveau ) ) {
137
		return;
138
	}
139
140
	$bp_nouveau->members = new BP_Nouveau_Members();
141
}
142
add_action( 'bp_nouveau_includes', 'bp_nouveau_members', 5, 1 );
143