PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
gamipress / integrations / mailerpress / includes / functions.php

functions.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.5, at integrations/mailerpress/includes/functions.php

195 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions
4 *
5 * @package GamiPress\MailerPress\Functions
6 * @since 1.0.0
7 */
8
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Overrides GamiPress AJAX Helper for selecting posts
14 *
15 * @since 1.0.0
16 */
17 function gamipress_mailerpress_ajax_get_posts() {
18
19 // Security check, forces to die if not security passed
20 check_ajax_referer( 'gamipress_admin', 'nonce' );
21
22 // Check if user can manage GamiPress
23 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
24 wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) );
25 }
26
27 global $wpdb;
28
29 $results = array();
30
31 // Pull back the search string
32 $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : '';
33
34 if( isset( $_REQUEST['post_type'] ) && in_array( 'mailerpress_tags', $_REQUEST['post_type'] ) ) {
35
36 $tags = gamipress_mailerpress_get_tags();
37
38 foreach ( $tags as $tag ) {
39
40 if( ! empty( $search ) ) {
41 if( strpos( strtolower( $tag['name'] ), strtolower( $search ) ) === false ) {
42 continue;
43 }
44 }
45
46 // Results should meet same structure like posts
47 $results[] = array(
48 'ID' => $tag['id'],
49 'post_title' => $tag['name'],
50 );
51
52 }
53
54 // Return our results
55 wp_send_json_success( $results );
56 die;
57
58 } else if( isset( $_REQUEST['post_type'] ) && in_array( 'mailerpress_lists', $_REQUEST['post_type'] ) ) {
59
60 $lists = gamipress_mailerpress_get_lists();
61
62 foreach ( $lists as $list ) {
63
64 if( ! empty( $search ) ) {
65 if( strpos( strtolower( $list['name'] ), strtolower( $search ) ) === false ) {
66 continue;
67 }
68 }
69
70 // Results should meet same structure like posts
71 $results[] = array(
72 'ID' => $list['id'],
73 'post_title' => $list['name'],
74 );
75
76 }
77
78 // Return our results
79 wp_send_json_success( $results );
80 die;
81
82 }
83
84 }
85 add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_mailerpress_ajax_get_posts', 5 );
86
87 /**
88 * Get the tags
89 *
90 * @since 1.0.0
91 *
92 * @return array
93 */
94 function gamipress_mailerpress_get_tags( ){
95
96 $tags = array();
97
98 // Bail if class does not exist
99 if( ! class_exists ( 'MailerPress\Models\Tags' ) )
100 return;
101
102 // Get tags data
103 $tags_data = new MailerPress\Models\Tags;
104 $all_tags = $tags_data->getAll();
105
106 foreach ( $all_tags as $tag ) {
107
108 $tags[] = array(
109 'id' => $tag->tag_id,
110 'name' => $tag->name,
111 );
112 }
113
114 return $tags;
115
116 }
117
118 /**
119 * Get the tag title
120 *
121 * @since 1.0.0
122 *
123 * @param int $tag_id
124 *
125 * @return string|null
126 */
127 function gamipress_mailerpress_get_tag_title( $tag_id ) {
128
129 // Empty title if no ID provided
130 if( absint( $tag_id ) === 0 ) {
131 return '';
132 }
133
134 global $wpdb;
135
136 $tag_name = $wpdb->get_var( "SELECT name FROM {$wpdb->prefix}mailerpress_tags WHERE tag_id = {$tag_id}" );
137
138 return $tag_name;
139
140 }
141
142 /**
143 * Get the lists
144 *
145 * @since 1.0.0
146 *
147 * @return array
148 */
149 function gamipress_mailerpress_get_lists( ){
150
151 $lists = array();
152
153 // Bail if class does not exist
154 if( ! class_exists ( 'MailerPress\Models\Lists' ) )
155 return;
156
157 // Get lists data
158 $lists_data = new MailerPress\Models\Lists;
159 $all_lists = $lists_data->getLists();
160
161 foreach ( $all_lists as $list ) {
162 $lists[] = array(
163 'id' => $list['list_id'],
164 'name' => $list['name'],
165 );
166 }
167
168 return $lists;
169
170 }
171
172 /**
173 * Get the list title
174 *
175 * @since 1.0.0
176 *
177 * @param int $list_id
178 *
179 * @return string|null
180 */
181 function gamipress_mailerpress_get_list_title( $list_id ) {
182
183 // Empty title if no ID provided
184 if( absint( $list_id ) === 0 ) {
185 return '';
186 }
187
188 global $wpdb;
189
190 $list_name = $wpdb->get_var( "SELECT name FROM {$wpdb->prefix}mailerpress_lists WHERE list_id = {$list_id}" );
191
192 return $list_name;
193
194 }
195