PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.0
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.0
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / admin / campaign-builder / access.php

access.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.0, at includes/admin/campaign-builder/access.php

154 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper functions to work with licenses, permissions and capabilities.
4 *
5 * @package Charitable
6 * @since 1.8.0
7 * @phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found
8 */
9
10 /**
11 * Search for posts editable by user.
12 *
13 * @since 1.8.0
14 *
15 * @param string $search_term Optional search term. Default ''.
16 * @param array $args Args {
17 * Optional. An array of arguments.
18 *
19 * @type string $post_type Post type to search for.
20 * @type string[] $post_status Post status to search for.
21 * @type int $count Number of results to return. Default 20.
22 * }
23 *
24 * @return array
25 * @noinspection PhpTernaryExpressionCanBeReducedToShortVersionInspection
26 * @noinspection ElvisOperatorCanBeUsedInspection
27 */
28 function charitable_campaign_search_posts( $search_term = '', $args = [] ) {
29
30 global $wpdb;
31
32 $default_args = [
33 'post_type' => 'page',
34 'post_status' => [ 'publish' ],
35 'count' => 20,
36 ];
37 $args = wp_parse_args( $args, $default_args );
38
39 // @todo: add trash access capabilities to MySQL.
40 // See edit_post/edit_page case in map_meta_cap().
41 $args['post_status'] = array_diff( $args['post_status'], [ 'trash' ] );
42
43 $user = wp_get_current_user();
44 $user_id = $user ? $user->ID : 0;
45 $post_type = get_post_type_object( $args['post_type'] );
46
47 if ( ! $user_id || ! $post_type || $args['count'] <= 0 ) {
48 return [];
49 }
50
51 $last_changed = wp_cache_get_last_changed( 'posts' );
52 $key = __FUNCTION__ . ":$search_term:$last_changed";
53 $cache_posts = wp_cache_get( $key, '', false, $found );
54
55 if ( $found ) {
56 return $cache_posts;
57 }
58
59 $post_title_where = $search_term ? $wpdb->prepare(
60 'post_title LIKE %s AND',
61 '%' . $wpdb->esc_like( $search_term ) . '%'
62 ) :
63 '';
64
65 $post_statuses = array_intersect( array_keys( get_post_statuses() ), $args['post_status'] );
66 $post_statuses = charitable_wpdb_prepare_in( $post_statuses );
67 $policy_id = (int) get_option( 'wp_page_for_privacy_policy' );
68 $can_delete_published_posts = (int) $user->has_cap( $post_type->cap->delete_published_posts );
69 $can_delete_posts = (int) $user->has_cap( $post_type->cap->delete_posts );
70 $can_delete_others_posts = (int) $user->has_cap( $post_type->cap->delete_others_posts );
71 $can_delete_private_posts = (int) $user->has_cap( $post_type->cap->delete_private_posts );
72 $can_edit_policy = (int) $user->has_cap( map_meta_cap( 'manage_privacy_options', $user_id )[0] );
73
74 // For the case when user is post author.
75 $capability_author_where = "post_author = $user_id AND
76 ( ( post_status IN ( 'publish', 'future' ) AND $can_delete_published_posts ) OR
77 ( ( post_status NOT IN ( 'publish', 'future', 'trash' ) ) AND $can_delete_posts )
78 )";
79
80 // For the case when accessing someone other's post.
81 $capability_other_where = "post_author != $user_id AND
82 $can_delete_others_posts AND
83 ( ( post_status IN ( 'publish', 'future' ) AND $can_delete_published_posts ) OR
84 ( ( post_status IN ( 'private' ) ) AND $can_delete_private_posts )
85 )";
86
87 // For privacy policy page.
88 $capability_policy_where = "ID = $policy_id AND $can_edit_policy";
89
90 $capability_where = '( ' .
91 '(' . $capability_author_where . ') OR ' .
92 '(' . $capability_other_where . ') OR ' .
93 '(' . $capability_policy_where . ')' .
94 ' )';
95
96 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
97 $posts = $wpdb->get_results(
98 $wpdb->prepare(
99 "SELECT ID, post_title, post_author
100 FROM $wpdb->posts
101 WHERE $post_title_where
102 post_type = '{$args['post_type']}' AND
103 post_status IN ( $post_statuses ) AND
104 $capability_where
105 ORDER BY post_title LIMIT %d",
106 absint( $args['count'] )
107 )
108 );
109 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
110
111 $posts = $posts ? $posts : [];
112 $posts = array_map(
113 static function ( $post ) {
114 $post->post_title = charitable_get_post_title( $post );
115
116 unset( $post->post_author );
117
118 return $post;
119 },
120 $posts
121 );
122
123 wp_cache_set( $key, $posts );
124
125 return $posts;
126 }
127
128 /**
129 * Search pages by search term and return an array containing
130 * `value` and `label` which is the post ID and post title respectively.
131 *
132 * @since 1.7.9
133 *
134 * @param string $search_term The search term.
135 * @param array $args Optional. An array of arguments.
136 *
137 * @return array
138 */
139 function charitable_search_pages_for_dropdown( $search_term, $args = [] ) {
140
141 $search_results = charitable_campaign_search_posts( $search_term, $args );
142 $result_pages = [];
143
144 // Prepare for ChoicesJS render.
145 foreach ( $search_results as $search_result ) {
146 $result_pages[] = [
147 'value' => absint( $search_result->ID ),
148 'label' => esc_html( $search_result->post_title ),
149 ];
150 }
151
152 return $result_pages;
153 }
154