| @@ -1,0 +1,120 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Requirements | |
| 4 | + * | |
| 5 | + * @package GamiPress\wpForo\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_wpforo_requirement_object( $requirement, $requirement_id ) { | |
| 20 | + | |
| 21 | + // Forums | |
| 22 | + if( isset( $requirement['trigger_type'] ) | |
| 23 | + && ( $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_new_post' | |
| 24 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_new_topic' | |
| 25 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_like_post' | |
| 26 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_dislike_post' | |
| 27 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_vote_up_post' | |
| 28 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_vote_down_post' | |
| 29 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_answer_question' ) ) { | |
| 30 | + // Forum ID | |
| 31 | + $requirement['wpf_forum'] = get_post_meta( $requirement_id, '_gamipress_wpforo_forum', true ); | |
| 32 | + } | |
| 33 | + | |
| 34 | + // Topics | |
| 35 | + if( isset( $requirement['trigger_type'] ) | |
| 36 | + && ( $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_new_post' | |
| 37 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_like_post' | |
| 38 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_dislike_post' | |
| 39 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_vote_up_post' | |
| 40 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_vote_down_post' ) ) { | |
| 41 | + | |
| 42 | + // Topic ID | |
| 43 | + $requirement['wpf_topic'] = get_post_meta( $requirement_id, '_gamipress_wpforo_topic', true ); | |
| 44 | + } | |
| 45 | + | |
| 46 | + return $requirement; | |
| 47 | +} | |
| 48 | +add_filter( 'gamipress_requirement_object', 'gamipress_wpforo_requirement_object', 10, 2 ); | |
| 49 | + | |
| 50 | +/** | |
| 51 | + * Custom field on requirements UI | |
| 52 | + * | |
| 53 | + * @since 1.0.0 | |
| 54 | + * | |
| 55 | + * @param $requirement_id | |
| 56 | + * @param $post_id | |
| 57 | + */ | |
| 58 | +function gamipress_wpforo_requirement_ui_fields( $requirement_id, $post_id ) { | |
| 59 | + | |
| 60 | + $forum_id = get_post_meta( $requirement_id, '_gamipress_wpforo_forum', true ); | |
| 61 | + $topic_id = get_post_meta( $requirement_id, '_gamipress_wpforo_topic', true ); | |
| 62 | + | |
| 63 | + ?> | |
| 64 | + <select class="wpf-forum wpf-forum-<?php echo $requirement_id; ?>"> | |
| 65 | + <?php if( ! empty( $forum_id ) ) : | |
| 66 | + $achievement_post_title = gamipress_wpforo_get_forum_title( $forum_id ); ?> | |
| 67 | + <option value="<?php esc_attr_e( $forum_id ); ?>" selected="selected"><?php echo $achievement_post_title; ?> (#<?php echo $forum_id; ?>)</option> | |
| 68 | + <?php endif; ?> | |
| 69 | + </select> | |
| 70 | + | |
| 71 | + <select class="wpf-topic wpf-topic-<?php echo $requirement_id; ?>"> | |
| 72 | + <?php if( ! empty( $topic_id ) ) : | |
| 73 | + $achievement_post_title = gamipress_wpforo_get_topic_title( $topic_id ); ?> | |
| 74 | + <option value="<?php esc_attr_e( $topic_id ); ?>" selected="selected"><?php echo $achievement_post_title; ?> (#<?php echo $topic_id; ?>)</option> | |
| 75 | + <?php endif; ?> | |
| 76 | + </select> | |
| 77 | + <?php | |
| 78 | +} | |
| 79 | +add_action( 'gamipress_requirement_ui_html_after_achievement_post', 'gamipress_wpforo_requirement_ui_fields', 10, 2 ); | |
| 80 | + | |
| 81 | +/** | |
| 82 | + * Custom handler to save the form on requirements UI | |
| 83 | + * | |
| 84 | + * @param $requirement_id | |
| 85 | + * @param $requirement | |
| 86 | + */ | |
| 87 | +function gamipress_wpforo_ajax_update_requirement( $requirement_id, $requirement ) { | |
| 88 | + | |
| 89 | + $requirement = wp_parse_args( $requirement, array( | |
| 90 | + 'wpf_forum' => '', | |
| 91 | + 'wpf_topic' => '', | |
| 92 | + ) ); | |
| 93 | + | |
| 94 | + // Forums | |
| 95 | + if( isset( $requirement['trigger_type'] ) | |
| 96 | + && ( $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_new_post' | |
| 97 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_new_topic' | |
| 98 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_like_post' | |
| 99 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_dislike_post' | |
| 100 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_vote_up_post' | |
| 101 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_vote_down_post' | |
| 102 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_forum_answer_question' ) ) { | |
| 103 | + | |
| 104 | + // Forum ID | |
| 105 | + update_post_meta( $requirement_id, '_gamipress_wpforo_forum', $requirement['wpf_forum'] ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + // Topics | |
| 109 | + if( isset( $requirement['trigger_type'] ) | |
| 110 | + && ( $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_new_post' | |
| 111 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_like_post' | |
| 112 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_dislike_post' | |
| 113 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_vote_up_post' | |
| 114 | + || $requirement['trigger_type'] === 'gamipress_wpforo_specific_topic_vote_down_post' ) ) { | |
| 115 | + | |
| 116 | + // Topic ID | |
| 117 | + update_post_meta( $requirement_id, '_gamipress_wpforo_topic', $requirement['wpf_topic'] ); | |
| 118 | + } | |
| 119 | +} | |
| 120 | +add_action( 'gamipress_ajax_update_requirement', 'gamipress_wpforo_ajax_update_requirement', 10, 2 ); | |