← All changes
|
integrations/shortlinkspro/includes/requirements.php
+101
-0
7.8.9
→
8.0.5
View file →
| @@ -1,0 +1,101 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Requirements | |
| 4 | + * | |
| 5 | + * @package GamiPress\ShortLinksPro\Requirements | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Add the form field to the requirement object | |
| 13 | + * | |
| 14 | + * @param $requirement | |
| 15 | + * @param $requirement_id | |
| 16 | + * | |
| 17 | + * @return array | |
| 18 | + */ | |
| 19 | +function gamipress_shortlinkspro_requirement_object( $requirement, $requirement_id ) { | |
| 20 | + | |
| 21 | + if( isset( $requirement['trigger_type'] ) | |
| 22 | + && ( $requirement['trigger_type'] === 'gamipress_shortlinkspro_click_specific_category') ) { | |
| 23 | + // Field form | |
| 24 | + $requirement['shortlinkspro_category'] = get_post_meta( $requirement_id, '_gamipress_shortlinkspro_category', true ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + if( isset( $requirement['trigger_type'] ) | |
| 28 | + && ( $requirement['trigger_type'] === 'gamipress_shortlinkspro_click_specific_tag') ) { | |
| 29 | + // Field form | |
| 30 | + $requirement['shortlinkspro_tag'] = get_post_meta( $requirement_id, '_gamipress_shortlinkspro_tag', true ); | |
| 31 | + } | |
| 32 | + | |
| 33 | + return $requirement; | |
| 34 | +} | |
| 35 | +add_filter( 'gamipress_requirement_object', 'gamipress_shortlinkspro_requirement_object', 10, 2 ); | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * Form field on requirements UI | |
| 39 | + * | |
| 40 | + * @param $requirement_id | |
| 41 | + * @param $post_id | |
| 42 | + */ | |
| 43 | +function gamipress_shortlinkspro_requirement_ui_fields( $requirement_id, $post_id ) { | |
| 44 | + | |
| 45 | + // Get the categories | |
| 46 | + $categories = gamipress_shortlinkspro_get_link_categories(); | |
| 47 | + $selected_category = get_post_meta( $requirement_id, '_gamipress_shortlinkspro_category', true ); | |
| 48 | + | |
| 49 | + // Get the tags | |
| 50 | + $tags = gamipress_shortlinkspro_get_link_tags(); | |
| 51 | + $selected_tag = get_post_meta( $requirement_id, '_gamipress_shortlinkspro_tag', true ); ?> | |
| 52 | + | |
| 53 | + <span class="shortlinkspro-category"> | |
| 54 | + <select> | |
| 55 | + <?php foreach( $categories as $category ) : ?> | |
| 56 | + <option value="<?php echo $category->id; ?>" <?php selected( $selected_category, $category->id ); ?>><?php echo $category->name; ?></option> | |
| 57 | + <?php endforeach; ?> | |
| 58 | + </select> | |
| 59 | + </span> | |
| 60 | + | |
| 61 | + <span class="shortlinkspro-tag"> | |
| 62 | + <select> | |
| 63 | + <?php foreach( $tags as $tag ) : ?> | |
| 64 | + <option value="<?php echo $tag->id; ?>" <?php selected( $selected_tag, $tag->id ); ?>><?php echo $tag->name; ?></option> | |
| 65 | + <?php endforeach; ?> | |
| 66 | + </select> | |
| 67 | + </span> | |
| 68 | + | |
| 69 | + <?php | |
| 70 | +} | |
| 71 | +add_action( 'gamipress_requirement_ui_html_after_achievement_post', 'gamipress_shortlinkspro_requirement_ui_fields', 10, 2 ); | |
| 72 | + | |
| 73 | +/** | |
| 74 | + * Custom handler to save the form on requirements UI | |
| 75 | + * | |
| 76 | + * @param $requirement_id | |
| 77 | + * @param $requirement | |
| 78 | + */ | |
| 79 | +function gamipress_shortlinkspro_ajax_update_requirement( $requirement_id, $requirement ) { | |
| 80 | + | |
| 81 | + $requirement = wp_parse_args( $requirement, array( | |
| 82 | + 'shortlinkspro_category' => '', | |
| 83 | + 'shortlinkspro_tag' => '', | |
| 84 | + ) ); | |
| 85 | + | |
| 86 | + if( isset( $requirement['trigger_type'] ) | |
| 87 | + && ( $requirement['trigger_type'] === 'gamipress_shortlinkspro_click_specific_category' ) ) { | |
| 88 | + | |
| 89 | + // Category | |
| 90 | + update_post_meta( $requirement_id, '_gamipress_shortlinkspro_category', $requirement['shortlinkspro_category'] ); | |
| 91 | + } | |
| 92 | + | |
| 93 | + if( isset( $requirement['trigger_type'] ) | |
| 94 | + && ( $requirement['trigger_type'] === 'gamipress_shortlinkspro_click_specific_tag' ) ) { | |
| 95 | + | |
| 96 | + // Tag | |
| 97 | + update_post_meta( $requirement_id, '_gamipress_shortlinkspro_tag', $requirement['shortlinkspro_tag'] ); | |
| 98 | + } | |
| 99 | + | |
| 100 | +} | |
| 101 | +add_action( 'gamipress_ajax_update_requirement', 'gamipress_shortlinkspro_ajax_update_requirement', 10, 2 ); | |