| @@ -1,0 +1,136 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Rules Engine | |
| 4 | + * | |
| 5 | + * @package GamiPress\wpForo\Rules_Engine | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Filter triggered requirements to reduce the number of requirements to check by the awards engine | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @param array $triggered_requirements | |
| 17 | + * @param integer $user_id | |
| 18 | + * @param string $trigger | |
| 19 | + * @param integer $site_id | |
| 20 | + * @param array $args | |
| 21 | + * | |
| 22 | + * @return array | |
| 23 | + */ | |
| 24 | +/* | |
| 25 | +function gamipress_wpforo_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) { | |
| 26 | + | |
| 27 | + $new_requirements = array(); | |
| 28 | + | |
| 29 | + foreach( $triggered_requirements as $i => $requirement ) { | |
| 30 | + | |
| 31 | + // Skip item | |
| 32 | + if( ! gamipress_wpforo_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) { | |
| 33 | + continue; | |
| 34 | + } | |
| 35 | + | |
| 36 | + // Keep the requirement on the list of requirements to check by the awards engine | |
| 37 | + $new_requirements[] = $requirement; | |
| 38 | + | |
| 39 | + } | |
| 40 | + | |
| 41 | + return $new_requirements; | |
| 42 | + | |
| 43 | +} | |
| 44 | +add_filter( 'gamipress_get_triggered_requirements', 'gamipress_wpforo_filter_triggered_requirements', 20, 5 ); | |
| 45 | +*/ | |
| 46 | +/** | |
| 47 | + * Checks if an user is allowed to work on a given requirement related to a minimum of score | |
| 48 | + * | |
| 49 | + * @since 1.0.0 | |
| 50 | + * | |
| 51 | + * @param bool $return The default return value | |
| 52 | + * @param int $user_id The given user's ID | |
| 53 | + * @param int $requirement_id The given requirement's post ID | |
| 54 | + * @param string $trigger The trigger triggered | |
| 55 | + * @param int $site_id The site id | |
| 56 | + * @param array $args Arguments of this trigger | |
| 57 | + * | |
| 58 | + * @return bool True if user has access to the requirement, false otherwise | |
| 59 | + */ | |
| 60 | +function gamipress_wpforo_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) { | |
| 61 | + | |
| 62 | + // If we're not working with a requirement, bail here | |
| 63 | + if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) | |
| 64 | + return $return; | |
| 65 | + | |
| 66 | + // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet) | |
| 67 | + if( ! $return ) | |
| 68 | + return $return; | |
| 69 | + | |
| 70 | + // Send back our eligibility | |
| 71 | + return gamipress_wpforo_check_if_meets_requirements( $requirement_id, $trigger, $args ); | |
| 72 | + | |
| 73 | +} | |
| 74 | +add_filter( 'user_has_access_to_achievement', 'gamipress_wpforo_user_has_access_to_achievement', 10, 6 ); | |
| 75 | + | |
| 76 | +/** | |
| 77 | + * Checks if given requirement meets the requirements of triggered event | |
| 78 | + * | |
| 79 | + * @since 1.0.0 | |
| 80 | + * | |
| 81 | + * @param int $requirement_id | |
| 82 | + * @param string $trigger | |
| 83 | + * @param array $args | |
| 84 | + * | |
| 85 | + * @return bool | |
| 86 | + */ | |
| 87 | +function gamipress_wpforo_check_if_meets_requirements( $requirement_id, $trigger, $args ) { | |
| 88 | + | |
| 89 | + // Initialize the return value | |
| 90 | + $return = true; | |
| 91 | + | |
| 92 | + // Check forum ID | |
| 93 | + if( $trigger === 'gamipress_wpforo_specific_forum_new_post' | |
| 94 | + || $trigger === 'gamipress_wpforo_specific_forum_like_post' | |
| 95 | + || $trigger === 'gamipress_wpforo_specific_forum_dislike_post' | |
| 96 | + || $trigger === 'gamipress_wpforo_specific_forum_vote_up_post' | |
| 97 | + || $trigger === 'gamipress_wpforo_specific_forum_vote_down_post' | |
| 98 | + || $trigger === 'gamipress_wpforo_specific_forum_answer_question' ) { | |
| 99 | + | |
| 100 | + $forum_id = $args[3]; | |
| 101 | + | |
| 102 | + $required_forum_id = get_post_meta( $requirement_id, '_gamipress_wpforo_forum', true ); | |
| 103 | + | |
| 104 | + // Check if forum ID matches the required one | |
| 105 | + $return = (bool) ( $forum_id === $required_forum_id ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + // Check forum ID when a topic is created in specific forum | |
| 109 | + if( $trigger === 'gamipress_wpforo_specific_forum_new_topic' ) { | |
| 110 | + | |
| 111 | + $forum_id = $args[2]; | |
| 112 | + | |
| 113 | + $required_forum_id = get_post_meta( $requirement_id, '_gamipress_wpforo_forum', true ); | |
| 114 | + | |
| 115 | + // Check if forum ID matches the required one | |
| 116 | + $return = (bool) ( $forum_id === $required_forum_id ); | |
| 117 | + } | |
| 118 | + | |
| 119 | + // Check topic ID | |
| 120 | + if( $trigger === 'gamipress_wpforo_specific_topic_new_post' | |
| 121 | + || $trigger === 'gamipress_wpforo_specific_topic_like_post' | |
| 122 | + || $trigger === 'gamipress_wpforo_specific_topic_dislike_post' | |
| 123 | + || $trigger === 'gamipress_wpforo_specific_topic_vote_up_post' | |
| 124 | + || $trigger === 'gamipress_wpforo_specific_topic_vote_down_post' ) { | |
| 125 | + | |
| 126 | + $topic_id = $args[2]; | |
| 127 | + | |
| 128 | + $required_topic_id = get_post_meta( $requirement_id, '_gamipress_wpforo_topic', true ); | |
| 129 | + | |
| 130 | + // Check if topic ID matches the required one | |
| 131 | + $return = (bool) ( $topic_id === $required_topic_id ); | |
| 132 | + } | |
| 133 | + | |
| 134 | + return $return; | |
| 135 | + | |
| 136 | +} | |