PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.9.7
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.9.7
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 / includes / admin / meta-boxes / default-badge.php

default-badge.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.9.7, at includes/admin/meta-boxes/default-badge.php

376 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Deafault Badge
4 *
5 * @package GamiPress\Admin\Default_Badge
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 7.6.3
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Default badges images
14 *
15 * @since 7.6.3
16 *
17 * @return array
18 */
19 function gamipress_default_badges_images() {
20
21 return apply_filters( 'gamipress_default_badges_images', array(
22 'gamipress-material-1' => array(
23 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-material-1.png',
24 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-material-1.png'
25 ),
26 'gamipress-material-2' => array(
27 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-material-2.png',
28 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-material-2.png'
29 ),
30 'gamipress-material-3' => array(
31 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-material-3.png',
32 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-material-3.png'
33 ),
34 'gamipress-material-4' => array(
35 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-material-4.png',
36 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-material-4.png'
37 ),
38 'gamipress-pastel-1' => array(
39 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-pastel-1.png',
40 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-pastel-1.png'
41 ),
42 'gamipress-pastel-2' => array(
43 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-pastel-2.png',
44 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-pastel-2.png'
45 ),
46 'gamipress-pastel-3' => array(
47 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-pastel-3.png',
48 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-pastel-3.png'
49 ),
50 'gamipress-pastel-4' => array(
51 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-pastel-4.png',
52 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-pastel-4.png'
53 ),
54 'gamipress-vintage-1' => array(
55 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-vintage-1.png',
56 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-vintage-1.png'
57 ),
58 'gamipress-vintage-2' => array(
59 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-vintage-2.png',
60 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-vintage-2.png'
61 ),
62 'gamipress-vintage-3' => array(
63 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-vintage-3.png',
64 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-vintage-3.png'
65 ),
66 'gamipress-vintage-4' => array(
67 'file' => GAMIPRESS_DIR . 'assets/badges/gamipress-vintage-4.png',
68 'url' => GAMIPRESS_URL . 'assets/badges/gamipress-vintage-4.png'
69 ),
70 ) );
71
72 }
73
74 /**
75 * Default points images
76 *
77 * @since 7.6.3
78 *
79 * @return array
80 */
81 function gamipress_default_points_images() {
82
83 return apply_filters( 'gamipress_default_points_images', array(
84 'gamipress-heart' => array(
85 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-heart.png',
86 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-heart.png'
87 ),
88 'gamipress-star' => array(
89 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-star.png',
90 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-star.png'
91 ),
92 'gamipress-brain' => array(
93 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-brain.png',
94 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-brain.png'
95 ),
96 'gamipress-8-ball' => array(
97 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-8-ball.png',
98 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-8-ball.png'
99 ),
100 'gamipress-credit' => array(
101 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-credit.png',
102 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-credit.png'
103 ),
104 'gamipress-energy' => array(
105 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-energy.png',
106 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-energy.png'
107 ),
108 'gamipress-game-coin' => array(
109 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-game-coin.png',
110 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-game-coin.png'
111 ),
112 'gamipress-chest' => array(
113 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-chest.png',
114 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-chest.png'
115 ),
116 'gamipress-ticket' => array(
117 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-ticket.png',
118 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-ticket.png'
119 ),
120 'gamipress-step' => array(
121 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-step.png',
122 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-step.png'
123 ),
124 'gamipress-sun' => array(
125 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-sun.png',
126 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-sun.png'
127 ),
128 'gamipress-star-coin' => array(
129 'file' => GAMIPRESS_DIR . 'assets/badges/points/gamipress-star-coin.png',
130 'url' => GAMIPRESS_URL . 'assets/badges/points/gamipress-star-coin.png'
131 ),
132 ) );
133
134 }
135
136 /**
137 * Text at top
138 *
139 * @since 7.6.3
140 *
141 * @return string
142 */
143 function gamipress_default_badges_text_top() {
144 return esc_html__( 'or choose an image from this list:', 'gamipress' );
145 }
146
147 /**
148 * Text at bottom
149 *
150 * @since 7.6.3
151 *
152 * @return string
153 */
154 function gamipress_default_badges_text_bottom() {
155 return esc_html__( 'Design your own images!', 'gamipress' );
156 }
157
158 /**
159 * Display default badges images
160 *
161 * @since 7.6.3
162 *
163 * @param string $content Featured image section
164 * @param int $post_id The post ID
165 *
166 * @return string
167 */
168 function gamipress_default_badges_images_meta_box( $content, $post_id ) {
169
170 $achievement_types = gamipress_get_achievement_types_slugs();
171 $point_types = gamipress_get_points_types_slugs();
172 $rank_types = gamipress_get_rank_types_slugs();
173 $general_types = array( 'points-type', 'achievement-type', 'rank-type' );
174
175 // All GamiPress post types
176 $allowed_post_types = array_merge( $achievement_types, $point_types, $rank_types, $general_types );
177 $post_type = get_post_type( $post_id );
178
179 if ( ! in_array( $post_type, $allowed_post_types, true ) ) {
180 return $content;
181 }
182
183 // Get default badges
184 if( $post_type === 'points-type' ) {
185 $images = gamipress_default_points_images();
186 } else {
187 $images = gamipress_default_badges_images();
188 }
189
190 if ( empty( $images ) ) {
191 return $content;
192 }
193
194 $content .= '<div class="gamipress-badge-builder-default-badges">';
195
196 $content .= '<p class="gamipress-badge-builder-default-badges-description-top">' . gamipress_default_badges_text_top() . '</p>';
197
198 $content .= '<div class="gamipress-badge-builder-default-badges-list">';
199 foreach ( $images as $img => $data ) {
200 $content .= sprintf(
201 '<img
202 src="%1$s"
203 class="gamipress-badge-builder-default-badge-thumb"
204 data-image="%2$s"
205 data-post="%3$d"
206 />',
207 esc_url( $data['url'] ),
208 esc_attr( $img ),
209 esc_attr( $post_id )
210 );
211 }
212
213 $content .= '<p class="gamipress-badge-builder-default-badges-description-bottom">'
214 . '<a href="' . esc_attr( admin_url( 'admin.php?page=gamipress_badge_builder' ) ) . '" target="_blank">' . gamipress_default_badges_text_bottom() . '</a>'
215 . '</p>';
216
217
218 $content .= '</div>';
219 $content .= '</div>';
220
221 return $content;
222 }
223 add_filter( 'admin_post_thumbnail_html', 'gamipress_default_badges_images_meta_box', 10, 2 );
224
225 /**
226 * Ajax function to set a default badge as featured image
227 *
228 * @since 7.6.3
229 *
230 */
231 function gamipress_ajax_badge_builder_set_featured_image() {
232
233 check_ajax_referer( 'gamipress_admin', 'nonce' );
234
235 if( ! current_user_can( gamipress_get_manager_capability() ) ) {
236 return;
237 }
238
239 $post_id = absint( $_POST['post_id'] );
240
241 // Bail if post does not exist
242 if( $post_id === 0 ) {
243 wp_send_json_error( 'post_not_found' );
244 }
245
246 $post_type = get_post_type( $post_id );
247
248 // Bail if post does not have a post type
249 if( $post_type === '' ) {
250 wp_send_json_error( 'post_type_not_found' );
251 }
252
253 $image = sanitize_text_field( $_POST['image'] );
254
255
256 // Get default badges
257 if( $post_type === 'points-type' ) {
258 $images = gamipress_default_points_images();
259 } else {
260 $images = gamipress_default_badges_images();
261 }
262
263 // Bail if image not found
264 if( ! isset( $images[$image] ) ) {
265 wp_send_json_error( 'file_not_found' );
266 }
267
268 $source_path = $images[$image]['file'];
269
270 // Bail if image file does not exist
271 if ( ! file_exists( $source_path ) ) {
272 wp_send_json_error( 'file_not_found' );
273 }
274
275 $filename = basename( $source_path );
276
277 $upload_dir = wp_upload_dir();
278
279 // Copy image to uploads
280 $destination = trailingslashit( $upload_dir['path'] ) . $filename;
281
282 if ( ! file_exists( $destination ) ) {
283 copy( $source_path, $destination );
284 }
285
286 // Search attachment
287 $relative_path = str_replace(
288 trailingslashit( $upload_dir['basedir'] ),
289 '',
290 $destination
291 );
292
293 global $wpdb;
294
295 $attachment_id = $wpdb->get_var( $wpdb->prepare(
296 "SELECT post_id
297 FROM {$wpdb->postmeta}
298 WHERE meta_key = '_wp_attached_file'
299 AND meta_value = %s
300 LIMIT 1",
301 $relative_path
302 ) );
303
304 // Insert new attachment if no exist
305 if ( ! $attachment_id ) {
306 $filetype = wp_check_filetype( $filename, null );
307
308 $attachment_id = wp_insert_attachment( array(
309 'post_mime_type' => $filetype['type'],
310 'post_title' => pathinfo( $filename, PATHINFO_FILENAME ),
311 'post_content' => '',
312 'post_status' => 'inherit',
313 ),
314 $destination, 0
315 );
316
317 if ( is_wp_error( $attachment_id ) ) {
318 wp_send_json_error( 'attachment_error' );
319 }
320
321 if( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
322 require_once ABSPATH . 'wp-admin/includes/image.php';
323 }
324
325 $attach_data = wp_generate_attachment_metadata(
326 $attachment_id,
327 $destination
328 );
329
330 wp_update_attachment_metadata( $attachment_id, $attach_data );
331 }
332
333 set_post_thumbnail( $post_id, $attachment_id );
334
335 wp_send_json_success( $attachment_id );
336 }
337 add_action( 'wp_ajax_gamipress_badge_builder_set_featured_image', 'gamipress_ajax_badge_builder_set_featured_image' );
338
339 /**
340 * Function to enqueue the script for default badges.
341 *
342 * @since 7.6.3
343 *
344 */
345 function gamipress_enqueue_block_default_badges() {
346
347 $achievement_types = gamipress_get_achievement_types_slugs();
348 $point_types = gamipress_get_points_types_slugs();
349 $rank_types = gamipress_get_rank_types_slugs();
350
351 // All GamiPress post types
352 $allowed_post_types = array_merge( $achievement_types, $point_types, $rank_types );
353
354 wp_enqueue_script(
355 'gamipress-default-badges-block',
356 GAMIPRESS_URL . 'assets/js/gamipress-default-badges-block.js',
357 array( 'wp-hooks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post' ),
358 '1.0',
359 true
360 );
361
362 wp_localize_script(
363 'gamipress-default-badges-block',
364 'gamipress_default_badges',
365 array(
366 'nonce' => wp_create_nonce( 'gamipress_admin' ),
367 'images' => gamipress_default_badges_images(),
368 'allowed_post_types' => $allowed_post_types,
369 'text_top' => gamipress_default_badges_text_top(),
370 'text_bottom' => gamipress_default_badges_text_bottom(),
371 'text_bottom_link' => admin_url( 'admin.php?page=gamipress_badge_builder' ),
372 )
373 );
374 }
375 add_action( 'enqueue_block_editor_assets', 'gamipress_enqueue_block_default_badges');
376