| @@ -1,0 +1,219 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * GamiPress Email Settings Shortcode | |
| 4 | + * | |
| 5 | + * @package GamiPress\Shortcodes\Shortcode\GamiPress_Email_Settings | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Register [gamipress_email_settings] shortcode | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +function gamipress_register_email_settings_shortcode() { | |
| 18 | + | |
| 19 | + gamipress_register_shortcode( 'gamipress_email_settings', array( | |
| 20 | + 'name' => __( 'Email Settings', 'gamipress' ), | |
| 21 | + 'description' => __( 'Display the user email notifications preferences for the GamiPress emails.', 'gamipress' ), | |
| 22 | + 'icon' => 'email', | |
| 23 | + 'group' => 'gamipress', | |
| 24 | + 'output_callback' => 'gamipress_email_settings_shortcode', | |
| 25 | + 'fields' => array( | |
| 26 | + 'groups' => array( | |
| 27 | + 'name' => __( 'Divide by group', 'gamipress' ), | |
| 28 | + 'tooltip' => __( 'Show email settings divided by group.', 'gamipress' ), | |
| 29 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 30 | + 'type' => 'checkbox', | |
| 31 | + 'classes' => 'gamipress-switch', | |
| 32 | + 'default' => 'yes', | |
| 33 | + ), | |
| 34 | + 'types' => array( | |
| 35 | + 'name' => __( 'Divide by type', 'gamipress' ), | |
| 36 | + 'tooltip' => __( 'Show email settings divided by type.', 'gamipress' ), | |
| 37 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 38 | + 'type' => 'checkbox', | |
| 39 | + 'classes' => 'gamipress-switch', | |
| 40 | + 'default' => 'yes', | |
| 41 | + ), | |
| 42 | + 'current_user' => array( | |
| 43 | + 'name' => __( 'Current User', 'gamipress' ), | |
| 44 | + 'tooltip' => __( 'Show the email settings of the current logged in user.', 'gamipress' ), | |
| 45 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 46 | + 'type' => 'checkbox', | |
| 47 | + 'classes' => 'gamipress-switch', | |
| 48 | + 'default' => 'yes', | |
| 49 | + ), | |
| 50 | + 'user_id' => array( | |
| 51 | + 'name' => __( 'User', 'gamipress' ), | |
| 52 | + 'tooltip' => __( 'Show the email settings of a specific user.', 'gamipress' ), | |
| 53 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 54 | + 'type' => 'select', | |
| 55 | + 'default' => '', | |
| 56 | + 'options_cb' => 'gamipress_options_cb_users' | |
| 57 | + ), | |
| 58 | + ), | |
| 59 | + ) ); | |
| 60 | + | |
| 61 | +} | |
| 62 | +add_action( 'init', 'gamipress_register_email_settings_shortcode' ); | |
| 63 | + | |
| 64 | +/** | |
| 65 | + * Logs List Shortcode | |
| 66 | + * | |
| 67 | + * @since 1.0.0 | |
| 68 | + * | |
| 69 | + * @param array $atts Shortcode attributes | |
| 70 | + * @param string $content Shortcode content | |
| 71 | + * | |
| 72 | + * @return string HTML markup | |
| 73 | + */ | |
| 74 | +function gamipress_email_settings_shortcode( $atts = array(), $content = '' ) { | |
| 75 | + | |
| 76 | + global $gamipress_template_args; | |
| 77 | + | |
| 78 | + // Initialize GamiPress template args global | |
| 79 | + $gamipress_template_args = array(); | |
| 80 | + | |
| 81 | + $shortcode = 'gamipress_email_settings'; | |
| 82 | + | |
| 83 | + $atts = shortcode_atts( array( | |
| 84 | + 'groups' => 'yes', | |
| 85 | + 'types' => 'yes', | |
| 86 | + 'current_user' => 'yes', | |
| 87 | + 'user_id' => '0', | |
| 88 | + ), $atts, $shortcode ); | |
| 89 | + | |
| 90 | + // Force to set current user as user ID | |
| 91 | + if( $atts['current_user'] === 'yes' ) { | |
| 92 | + $atts['user_id'] = get_current_user_id(); | |
| 93 | + } else if( absint( $atts['user_id'] ) === 0 ) { | |
| 94 | + $atts['user_id'] = get_current_user_id(); | |
| 95 | + } | |
| 96 | + | |
| 97 | + // Return if current user is a guest | |
| 98 | + if( $atts['user_id'] === 0 ) { | |
| 99 | + /** | |
| 100 | + * Filter to override the message for not logged in users | |
| 101 | + * | |
| 102 | + * @since 2.2.1 | |
| 103 | + * | |
| 104 | + * @param string $message Message to display to not logged in users | |
| 105 | + * @param array $atts Shortcode attributes | |
| 106 | + * @param string $content Shortcode content | |
| 107 | + */ | |
| 108 | + return apply_filters( 'gamipress_email_settings_shortcode_not_logged_in_message', __( 'Please, log in to view your email preferences.', 'gamipress' ), $atts, $content ); | |
| 109 | + } | |
| 110 | + | |
| 111 | + // Enqueue assets | |
| 112 | + gamipress_enqueue_scripts(); | |
| 113 | + | |
| 114 | + // GamiPress template args global | |
| 115 | + $gamipress_template_args = $atts; | |
| 116 | + | |
| 117 | + // Setup the email settings sections to display | |
| 118 | + $email_settings = array(); | |
| 119 | + | |
| 120 | + $email_settings['all'] = array( | |
| 121 | + 'label' => __( 'Gamification', 'gamipress' ), | |
| 122 | + 'settings' => array( | |
| 123 | + 'all' => __( 'Receive emails related to gamification', 'gamipress' ), | |
| 124 | + ) | |
| 125 | + ); | |
| 126 | + | |
| 127 | + // Add settings per group if enabled | |
| 128 | + if( $atts['groups'] === 'yes' ) { | |
| 129 | + | |
| 130 | + // Only add the points types section if any of its emails is enabled | |
| 131 | + if( ! gamipress_get_option( 'disable_points_award_completed_email', false ) || ! gamipress_get_option( 'disable_points_deduct_completed_email', false ) ) { | |
| 132 | + | |
| 133 | + $number_point_types = count( gamipress_get_points_types() ); | |
| 134 | + | |
| 135 | + $email_settings['points_types']['label'] = __( 'Points', 'gamipress' ); | |
| 136 | + | |
| 137 | + if ( ( absint( $number_point_types < 2 && $atts['types'] === 'no') ) || absint( $number_point_types >= 2 ) ) { | |
| 138 | + $email_settings['points_types']['settings']['points_types'] = __( 'Receive emails related to points balance movements', 'gamipress' ); | |
| 139 | + } | |
| 140 | + | |
| 141 | + // Add settings per type if enabled | |
| 142 | + if( $atts['types'] === 'yes' ) { | |
| 143 | + foreach ( gamipress_get_points_types() as $type => $data ) { | |
| 144 | + $email_settings['points_types']['settings']['points_types_' . $type] = sprintf( __( 'Receive emails related to %s balance movements', 'gamipress' ), $data['plural_name'] ); | |
| 145 | + } | |
| 146 | + } | |
| 147 | + | |
| 148 | + } | |
| 149 | + | |
| 150 | + // Only add the achievement types section if any of its emails is enabled | |
| 151 | + if( ! gamipress_get_option( 'disable_achievement_earned_email', false ) || ! gamipress_get_option( 'disable_step_completed_email', false ) ) { | |
| 152 | + | |
| 153 | + $number_achievement_types = count( gamipress_get_achievement_types() ); | |
| 154 | + | |
| 155 | + $email_settings['achievement_types']['label'] = __( 'Achievements', 'gamipress' ); | |
| 156 | + | |
| 157 | + if ( ( absint( $number_achievement_types < 2 && $atts['types'] === 'no') ) || absint( $number_achievement_types >= 2 ) ) { | |
| 158 | + $email_settings['achievement_types']['settings']['achievement_types'] = __( 'Receive emails related to achievements', 'gamipress' ); | |
| 159 | + } | |
| 160 | + | |
| 161 | + // Add settings per type if enabled | |
| 162 | + if( $atts['types'] === 'yes' ) { | |
| 163 | + foreach ( gamipress_get_achievement_types() as $type => $data ) { | |
| 164 | + $email_settings['achievement_types']['settings']['achievement_types_' . $type] = sprintf( __( 'Receive emails related to %s', 'gamipress' ), $data['plural_name'] ); | |
| 165 | + } | |
| 166 | + } | |
| 167 | + | |
| 168 | + } | |
| 169 | + | |
| 170 | + // Only add the rank types section if any of its emails is enabled | |
| 171 | + if( ! gamipress_get_option( 'disable_rank_earned_email', false ) || ! gamipress_get_option( 'disable_rank_requirement_completed_email', false ) ) { | |
| 172 | + | |
| 173 | + $number_rank_types = count( gamipress_get_rank_types() ); | |
| 174 | + | |
| 175 | + $email_settings['rank_types']['label'] = __( 'Ranks', 'gamipress' ); | |
| 176 | + | |
| 177 | + if ( ( absint( $number_rank_types < 2 && $atts['types'] === 'no') ) || absint( $number_rank_types >= 2 ) ) { | |
| 178 | + $email_settings['rank_types']['settings']['rank_types'] = __( 'Receive emails related to ranks', 'gamipress' ); | |
| 179 | + } | |
| 180 | + | |
| 181 | + // Add settings per type if enabled | |
| 182 | + if( $atts['types'] === 'yes' ) { | |
| 183 | + foreach ( gamipress_get_rank_types() as $type => $data ) { | |
| 184 | + $email_settings['rank_types']['settings']['rank_types_' . $type] = sprintf( __( 'Receive emails related to %s', 'gamipress' ), $data['plural_name'] ); | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + } | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Filter to override the email settings | |
| 193 | + * | |
| 194 | + * @since 2.2.1 | |
| 195 | + * | |
| 196 | + * @param array $email_settings The email settings to display | |
| 197 | + * @param array $atts Shortcode attributes | |
| 198 | + * @param string $content Shortcode content | |
| 199 | + */ | |
| 200 | + $email_settings = apply_filters( 'gamipress_email_settings_shortcode_settings', $email_settings, $atts, $content ); | |
| 201 | + | |
| 202 | + $gamipress_template_args['email_settings'] = $email_settings; | |
| 203 | + | |
| 204 | + ob_start(); | |
| 205 | + gamipress_get_template_part( 'email-settings' ); | |
| 206 | + $output = ob_get_clean(); | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Filter to override shortcode output | |
| 210 | + * | |
| 211 | + * @since 1.6.5 | |
| 212 | + * | |
| 213 | + * @param string $output Final output | |
| 214 | + * @param array $atts Shortcode attributes | |
| 215 | + * @param string $content Shortcode content | |
| 216 | + */ | |
| 217 | + return apply_filters( 'gamipress_email_settings_shortcode_output', $output, $atts, $content ); | |
| 218 | + | |
| 219 | +} | |