PluginProbe
Groups – Memberships and Access Control / 4.7.0
Groups – Memberships and Access Control v4.7.0
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / extra / class-groups-extra.php

class-groups-extra.php in Groups – Memberships and Access Control 4.7.0, at lib/extra/class-groups-extra.php

271 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-extra.php
4 *
5 * Copyright (c) "kento" Karim Rahimpur www.itthinx.com
6 *
7 * This code is released under the GNU General Public License.
8 * See COPYRIGHT.txt and LICENSE.txt.
9 *
10 * This code is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This header and all notices must be kept intact.
16 *
17 * @author Karim Rahimpur
18 * @package groups
19 * @since groups 2.1.2
20 */
21
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Compatibility actions, filters, etc as needed.
28 */
29 class Groups_Extra {
30
31 /**
32 * bbPress pre_get_posts filter priority.
33 *
34 * @since 4.1.0
35 *
36 * @var int
37 */
38 private static $bbp_pre_get_posts_priority = null;
39
40 /**
41 * Post status before bbPress alters.
42 *
43 * @since 4.1.0
44 *
45 * @var string|string[]
46 */
47 private static $bbp_pre_get_posts_post_status = null;
48
49 /**
50 * Early actions.
51 */
52 public static function boot() {
53 add_action( 'init', array( __CLASS__, 'init' ) );
54 add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
55 add_action( 'before_woocommerce_init', array( __CLASS__, 'before_woocommerce_init' ) );
56 }
57
58 /**
59 * Registers actions, filters ...
60 */
61 public static function init() {
62 add_filter( 'woocommerce_product_is_visible', array( __CLASS__, 'woocommerce_product_is_visible' ), 10, 2 );
63 add_filter( 'groups_comment_access_comment_count_where', array( __CLASS__, 'groups_comment_access_comment_count_where'), 10, 2 );
64 add_filter( 'groups_post_access_posts_where_query_get_post_types', array( __CLASS__, 'groups_post_access_posts_where_query_get_post_types' ), 10, 3 );
65 add_filter( 'woocommerce_duplicate_product_capability', array( __CLASS__, 'woocommerce_duplicate_product_capability' ) );
66 add_filter( 'groups_post_access_wp_count_posts_before_query', array( __CLASS__, 'groups_post_access_wp_count_posts_before_query' ), 10, 4 );
67 add_filter( 'groups_post_access_wp_count_posts_after_query', array( __CLASS__, 'groups_post_access_wp_count_posts_after_query' ), 10, 4 );
68 }
69
70 /**
71 * Add admin filters and actions.
72 *
73 * @since 4.0.0
74 */
75 public static function admin_init() {
76 add_action( 'current_screen', array( __CLASS__, 'current_screen' ), PHP_INT_MIN );
77 }
78
79 /**
80 * Up-sell and cross-sell products are obtained directly by their ids and
81 * no normal filters are executed that would hide them. This filter is used
82 * instead to determine the visibility.
83 *
84 * If at some point we had a get_post filter in WordPress, it could filter these
85 * and we wouldn't need this.
86 *
87 * @param boolean $visible
88 * @param int $product_id
89 *
90 * @return boolean
91 */
92 public static function woocommerce_product_is_visible( $visible, $product_id ) {
93 if ( $visible ) {
94 $visible = Groups_Post_Access::user_can_read_post( $product_id );
95 }
96 return $visible;
97 }
98
99 /**
100 * Take WooCommerce comment types into account.
101 *
102 * @param string $where
103 * @param int $post_id
104 *
105 * @return string
106 */
107 public static function groups_comment_access_comment_count_where( $where, $post_id ) {
108 if ( defined( 'WC_VERSION' ) ) {
109 $where .= " AND comment_type NOT IN ('order_note', 'webhook_delivery') ";
110 }
111 return $where;
112 }
113
114 /**
115 * Checks if the query is a wc_query for product_query; if $post_types is empty, it will assume the product type and return that.
116 *
117 * @param string|array $post_types current query post types
118 * @param string $where the where part of the query
119 * @param WP_Query $query the query
120 *
121 * @return string
122 */
123 public static function groups_post_access_posts_where_query_get_post_types( $post_types, $where, $query ) {
124 if (
125 empty( $post_types ) ||
126 is_string( $post_types ) && ( $post_types === '' ) ||
127 is_array( $post_types ) && ( count( $post_types ) === 0 )
128 ) {
129 $wc_query = $query->get( 'wc_query', null );
130 if ( ! empty( $wc_query ) && $wc_query === 'product_query' ) {
131 $post_types = 'product';
132 }
133 }
134 return $post_types;
135 }
136
137 /**
138 * Declare WooCommerce feature compatibility.
139 *
140 * @since 3.4.1
141 */
142 public static function before_woocommerce_init() {
143 // HPOS
144 if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
145 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', GROUPS_FILE, true );
146 }
147 }
148
149 /**
150 * Do not redirect to the add product tasklist if there are no products when filtering.
151 *
152 * @since 4.0.0
153 *
154 * Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Products's maybe_redirect_to_add_product_tasklist hooks on the current_screen action
155 */
156 public static function current_screen() {
157 global $wp_filter;
158
159 if ( function_exists( 'get_current_screen' ) ) {
160 $screen = get_current_screen();
161 if ( 'edit' === $screen->base && 'product' === $screen->post_type ) {
162 $filtering = false;
163 if ( class_exists( 'Groups_Post_Access_Legacy' ) ) {
164 $field = Groups_Post_Access_Legacy::POSTMETA_PREFIX . Groups_Post_Access_Legacy::READ_POST_CAPABILITY;
165 $filtering = !empty( groups_sanitize_get( $field ) );
166 }
167 $filtering = $filtering || !empty( groups_sanitize_get( Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ ) );
168 $filtering = $filtering || !empty( groups_sanitize_get( 'groups-woocommerce-product-groups' ) );
169 if ( $filtering ) {
170 foreach ( $wp_filter['current_screen']->callbacks as $priority => $callbacks ) {
171 foreach ( $callbacks as $callback => $data ) {
172 if ( strpos( $callback, 'maybe_redirect_to_add_product_tasklist' ) !== false ) {
173 remove_action( 'current_screen', $callback, $priority );
174 }
175 }
176 }
177 }
178 }
179 }
180 }
181
182 /**
183 * Do not allow users to duplicate restricted products if they lack restriction rights.
184 *
185 * @since 4.0.0
186 *
187 * @param string $capability
188 *
189 * @return string
190 */
191 public static function woocommerce_duplicate_product_capability( $capability ) {
192 global $post;
193 if (
194 isset( $post ) &&
195 is_object( $post ) &&
196 !empty( $post->post_type ) &&
197 $post->post_type === 'product'
198 ) {
199 // Groups_User::can() does not support the $object and additional $args
200 // so we cannot use it here to check whether the user can edit the product;
201 // instead we use Groups_User::current_user_can() which does support it
202 // $user = new Groups_User( get_current_user_id() );
203 // if ( !$user->can( 'edit_product', $post ) ) {
204 // $capability = 'do_not_allow';
205 // }
206 if ( !Groups_User::current_user_can( 'edit_product', $post ) ) {
207 $capability = 'do_not_allow';
208 }
209 }
210 return $capability;
211 }
212
213 /**
214 * Fires before get_posts query.
215 *
216 * @since 4.1.0
217 *
218 * @param array $query_args
219 * @param object $counts
220 * @param string $type
221 * @param string $perm
222 */
223 public static function groups_post_access_wp_count_posts_before_query( $query_args, $counts, $type, $perm ) {
224 // Workaound issue introduced by bbPress action on pre_get_posts which introduces additional types ... related https://bbpress.trac.wordpress.org/ticket/2512
225 if ( $type === 'forum' ) {
226 $priority = has_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility' );
227 if ( $priority !== false ) {
228 // Instead of removing the action, we revert the altered post_status:
229 // remove_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility', $priority );
230 add_action( 'pre_get_posts', array( __CLASS__, 'bbp_fix_pre_get_posts' ), $priority + 1 );
231 self::$bbp_pre_get_posts_priority = $priority;
232 self::$bbp_pre_get_posts_post_status = $query_args['post_status'];
233 }
234 }
235 }
236
237 /**
238 * Fires after get_posts query.
239 *
240 * @since 4.1.0
241 *
242 * @param array $query_args
243 * @param object $counts
244 * @param string $type
245 * @param string $perm
246 */
247 public static function groups_post_access_wp_count_posts_after_query( $query_args, $counts, $type, $perm ) {
248 if ( self::$bbp_pre_get_posts_priority !== null ) {
249 // see note in ...before... action
250 // add_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility', self::$bbp_pre_get_posts_priority );
251 remove_action( 'pre_get_posts', array( __CLASS__, 'bbp_fix_pre_get_posts' ), self::$bbp_pre_get_posts_priority + 1 );
252 self::$bbp_pre_get_posts_priority = null;
253 self::$bbp_pre_get_posts_post_status = null;
254 }
255 }
256
257 /**
258 * Reestablish altered post_status.
259 *
260 * @since 4.1.0
261 *
262 * @param WP_Query $query
263 */
264 public static function bbp_fix_pre_get_posts( $query ) {
265 if ( self::$bbp_pre_get_posts_post_status !== null ) {
266 $query->set( 'post_status', self::$bbp_pre_get_posts_post_status );
267 }
268 }
269 }
270 Groups_Extra::boot();
271