| 1 |
<?php |
| 2 |
/** |
| 3 |
* Help |
| 4 |
* |
| 5 |
* @package GamiPress\AI_Assistant\Help |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Render AI assistant help |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
function gamipress_ai_assistant_render_help() { |
| 18 |
$points_types = gamipress_get_points_types(); |
| 19 |
$achievements_types = gamipress_get_achievement_types(); |
| 20 |
$rank_types = gamipress_get_rank_types(); |
| 21 |
|
| 22 |
$achievement = gamipress_ai_assistant_get_random_post( array_keys( $achievements_types ) ); |
| 23 |
$achievement_id = ( $achievement ? $achievement->ID : 123 ); |
| 24 |
$achievement_title = ( $achievement ? $achievement->post_title : __( 'First Badge', 'gamipress' ) ); |
| 25 |
$achievement_singular = ( $achievement && isset( $achievements_types[$achievement->post_type] ) ? $achievements_types[$achievement->post_type]['singular_name'] : __( 'Badge', 'gamipress' ) ); |
| 26 |
|
| 27 |
$rank = gamipress_ai_assistant_get_random_post( array_keys( $rank_types ) ); |
| 28 |
$rank_id = ( $rank ? $rank->ID : 123 ); |
| 29 |
$rank_title = ( $rank ? $rank->post_title : __( 'Beginner', 'gamipress' ) ); |
| 30 |
$rank_singular = ( $rank && isset( $rank_types[$rank->post_type] ) ? $rank_types[$rank->post_type]['singular_name'] : __( 'Rank', 'gamipress' ) ); |
| 31 |
|
| 32 |
$user = wp_get_current_user(); |
| 33 |
$user_first = $user->user_login; |
| 34 |
$user_email = $user->user_email; |
| 35 |
$user_id = $user->ID; |
| 36 |
|
| 37 |
$abilities = array( |
| 38 |
'suggest-gamification' => array( |
| 39 |
'label' => __( 'Suggest Configurations', 'gamipress' ), |
| 40 |
'icon' => 'gamipress', |
| 41 |
'desc' => __( 'I can suggest, design and create all gamification elements for GamiPress.', 'gamipress' ), |
| 42 |
'prompts' => array( |
| 43 |
sprintf( __( 'Suggest 5 badges based on %1$s activities and create them as %2$s', 'gamipress' ), |
| 44 |
gamipress_ai_assistant_get_random_plugins( 2 ), |
| 45 |
gamipress_ai_assistant_get_random_plural( $achievements_types, __( 'badges', 'gamipress' ) ), |
| 46 |
), |
| 47 |
sprintf( __( 'Generate a points type with 3 awards and 2 deducts based on %1$s activities', 'gamipress' ), |
| 48 |
gamipress_ai_assistant_get_random_plugins( 1 ), |
| 49 |
), |
| 50 |
sprintf( __( 'Set up 5 progressive %1$s based on %2$s thresholds that increase by 100', 'gamipress' ), |
| 51 |
gamipress_ai_assistant_get_random_plural( $rank_types, __( 'ranks', 'gamipress' ) ), |
| 52 |
gamipress_ai_assistant_get_random_singular( $points_types, __( 'point', 'gamipress' ) ), |
| 53 |
), |
| 54 |
), |
| 55 |
), |
| 56 |
'create-types' => array( |
| 57 |
'label' => __( 'Create New Types', 'gamipress' ), |
| 58 |
'icon' => 'heart', |
| 59 |
'desc' => __( 'I\'m able to create new types (points, achievements and ranks).', 'gamipress' ) |
| 60 |
. ' ' . __( 'For points types, I can also define points awards and deductions in the same prompt.', 'gamipress' ) , |
| 61 |
'prompts' => array( |
| 62 |
sprintf( __( 'Add the "Points" points type with 3 awards based on %1$s', 'gamipress' ), |
| 63 |
gamipress_ai_assistant_get_random_plugins( 1 ), |
| 64 |
), |
| 65 |
__( 'Create the "Badge" achievement type', 'gamipress' ), |
| 66 |
__( 'Add a rank type named "Rank"', 'gamipress' ), |
| 67 |
), |
| 68 |
), |
| 69 |
'create-achievements-ranks' => array( |
| 70 |
'label' => __( 'Set Up Achievements & Ranks', 'gamipress' ), |
| 71 |
'icon' => 'admin-tools', |
| 72 |
'desc' => __( 'I can design, create and configure new achievements & ranks for your site.', 'gamipress' ), |
| 73 |
'prompts' => array( |
| 74 |
sprintf( __( 'Create the %1$s "Early Bird" given for logging 5 times', 'gamipress' ), |
| 75 |
gamipress_ai_assistant_get_random_singular( $achievements_types, __( 'badge', 'gamipress' ) ), |
| 76 |
), |
| 77 |
sprintf( __( 'Create a %1$s for visiting the site 3 times and commenting on %2$s', 'gamipress' ), |
| 78 |
gamipress_ai_assistant_get_random_singular( $rank_types, __( 'rank', 'gamipress' ) ), |
| 79 |
__( 'Sample Page', 'gamipress' ), |
| 80 |
), |
| 81 |
), |
| 82 |
), |
| 83 |
'create-points-awards-deducts' => array( |
| 84 |
'label' => __( 'Add Points Rules', 'gamipress' ), |
| 85 |
'icon' => 'flag', |
| 86 |
'desc' => __( 'I can add new rules to award and deduct points to your points types.', 'gamipress' ), |
| 87 |
'prompts' => array( |
| 88 |
sprintf( __( 'Add 2 awards based on %1$s to %2$s', 'gamipress' ), |
| 89 |
gamipress_ai_assistant_get_random_plugin(), |
| 90 |
gamipress_ai_assistant_get_random_plural( $points_types, __( 'points', 'gamipress' ) ), |
| 91 |
), |
| 92 |
sprintf( __( 'New rule: Deduct 25 %1$s for publishing a new post', 'gamipress' ), |
| 93 |
gamipress_ai_assistant_get_random_plural( $points_types, __( 'points', 'gamipress' ) ), |
| 94 |
$user_first, |
| 95 |
), |
| 96 |
), |
| 97 |
), |
| 98 |
'create-requirements' => array( |
| 99 |
'label' => __( 'Set Up Requirements', 'gamipress' ), |
| 100 |
'icon' => 'rank', |
| 101 |
'desc' => __( 'I can add new requirements to achievements and ranks.', 'gamipress' ), |
| 102 |
'prompts' => array( |
| 103 |
sprintf( __( 'Add a step to "%1$s" %2$s for log in 5 times', 'gamipress' ), |
| 104 |
$achievement_title, |
| 105 |
$achievement_singular, |
| 106 |
), |
| 107 |
sprintf( __( 'New requirement to "%1$s" %2$s: Earn 100 %3$s', 'gamipress' ), |
| 108 |
$rank_title, |
| 109 |
$rank_singular, |
| 110 |
gamipress_ai_assistant_get_random_plural( $points_types, __( 'points', 'gamipress' ) ), |
| 111 |
), |
| 112 |
sprintf( __( 'Set up the %1$s "%2$s" with 3 steps based on %3$s', 'gamipress' ), |
| 113 |
$achievement_singular, |
| 114 |
$achievement_title, |
| 115 |
gamipress_ai_assistant_get_random_plugin(), |
| 116 |
), |
| 117 |
), |
| 118 |
), |
| 119 |
'award-deduct-points' => array( |
| 120 |
'label' => __( 'Award/Deduct Points', 'gamipress' ), |
| 121 |
'icon' => 'star-filled', |
| 122 |
'desc' => __( 'I can update your users balance and register the movement on user earnings.', 'gamipress' ), |
| 123 |
'prompts' => array( |
| 124 |
sprintf( __( 'Award 10 %1$s to %2$s for commenting', 'gamipress' ), |
| 125 |
gamipress_ai_assistant_get_random_plural( $points_types, __( 'points', 'gamipress' ) ), |
| 126 |
$user_first, |
| 127 |
), |
| 128 |
sprintf( __( 'Deduct 10 %1$s to %2$s without register on earnings', 'gamipress' ), |
| 129 |
gamipress_ai_assistant_get_random_plural( $points_types, __( 'points', 'gamipress' ) ), |
| 130 |
$user_first, |
| 131 |
), |
| 132 |
), |
| 133 |
), |
| 134 |
'award-revoke-achievements-ranks' => array( |
| 135 |
'label' => __( 'Award/Revoke Achievements & Ranks', 'gamipress' ), |
| 136 |
'icon' => 'awards', |
| 137 |
'desc' => __( 'I can manage your users earnings easily.', 'gamipress' ), |
| 138 |
'prompts' => array( |
| 139 |
sprintf( __( 'Award the %1$s "%2$s" to %3$s', 'gamipress' ), |
| 140 |
$achievement_singular, |
| 141 |
$achievement_title, |
| 142 |
$user_first, |
| 143 |
), |
| 144 |
sprintf( __( 'Revoke the %1$s "%2$s" to %3$s', 'gamipress' ), |
| 145 |
$rank_singular, |
| 146 |
$rank_title, |
| 147 |
$user_first, |
| 148 |
), |
| 149 |
), |
| 150 |
), |
| 151 |
); |
| 152 |
|
| 153 |
$first_ability = array_keys( $abilities )[0]; |
| 154 |
|
| 155 |
?> |
| 156 |
|
| 157 |
<div class="gamipress-ai-assistant-help-presentation"> |
| 158 |
<?php _e( 'I\'m an <strong>AI assistant</strong> specially designed to help you manage <strong>GamiPress</strong> using natural language.', 'gamipress' ); ?> |
| 159 |
<br> |
| 160 |
<br> |
| 161 |
<?php esc_html_e( 'Here\'s what I can do:', 'gamipress' ); ?> |
| 162 |
</div> |
| 163 |
|
| 164 |
<?php foreach( $abilities as $ability => $data ) : ?> |
| 165 |
<div class="gamipress-ai-assistant-help-ability gamipress-ai-assistant-help-ability-<?php echo esc_attr( $ability ); ?> <?php echo ( $ability === $first_ability ? 'gamipress-ai-assistant-help-ability-open' : 'gamipress-ai-assistant-help-ability-close' ); ?>"> |
| 166 |
<div class="gamipress-ai-assistant-help-ability-label"><?php |
| 167 |
echo gamipress_dashicon( $data['icon'] ) |
| 168 |
. ' ' . esc_html( $data['label'] ) |
| 169 |
. ' ' . gamipress_dashicon( 'arrow-up-alt2' ); |
| 170 |
?></div> |
| 171 |
<div class="gamipress-ai-assistant-help-ability-desc" style="<?php echo ( $ability === $first_ability ? '' : 'display: none;' ); ?>"> |
| 172 |
<div class="gamipress-ai-assistant-help-ability-desc-text"><?php echo esc_html( $data['desc'] ); ?></div> |
| 173 |
<strong><small><?php esc_html_e( 'Examples', 'gamipress' ); ?></small></strong> |
| 174 |
<div class="gamipress-ai-assistant-help-ability-prompts"> |
| 175 |
<?php foreach( $data['prompts'] as $prompt ) : ?> |
| 176 |
<span class="gamipress-ai-assistant-prompt"><?php echo esc_html( $prompt ); ?></span> |
| 177 |
<div class="gamipress-ai-assistant-send-prompt cmb-tooltip" data-prompt="<?php echo esc_attr( $prompt ); ?>"> |
| 178 |
<?php echo gamipress_dashicon( 'edit' ); ?> |
| 179 |
<div class="cmb-tooltip-desc cmb-tooltip-top"><?php echo esc_html( __( 'Edit Prompt', 'gamipress' ) ); ?></div> |
| 180 |
</div> |
| 181 |
<div class="gamipress-ai-assistant-send-prompt cmb-tooltip" data-prompt="<?php echo esc_attr( $prompt ); ?>" data-send="true"> |
| 182 |
<?php echo gamipress_dashicon( 'share-alt2' ); ?> |
| 183 |
<div class="cmb-tooltip-desc cmb-tooltip-top"><?php echo esc_html( __( 'Send to Assistant', 'gamipress' ) ); ?></div> |
| 184 |
</div> |
| 185 |
<br> |
| 186 |
<?php endforeach; ?> |
| 187 |
</div> |
| 188 |
</div> |
| 189 |
</div> |
| 190 |
<?php endforeach; ?> |
| 191 |
|
| 192 |
<?php |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get a random singular name from GamiPress types |
| 197 |
* |
| 198 |
* @since 1.0.0 |
| 199 |
* |
| 200 |
* @param array $types |
| 201 |
* @param string $default |
| 202 |
* |
| 203 |
* @return string |
| 204 |
*/ |
| 205 |
function gamipress_ai_assistant_get_random_singular( $types, $default = '' ) { |
| 206 |
|
| 207 |
$rand = gamipress_assoc_array_rand( $types ); |
| 208 |
if( is_array( $rand ) ) return $rand['singular_name']; |
| 209 |
|
| 210 |
return $default; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get a random plural name from GamiPress types |
| 215 |
* |
| 216 |
* @since 1.0.0 |
| 217 |
* |
| 218 |
* @param array $types |
| 219 |
* @param string $default |
| 220 |
* |
| 221 |
* @return string |
| 222 |
*/ |
| 223 |
function gamipress_ai_assistant_get_random_plural( $types, $default = '' ) { |
| 224 |
|
| 225 |
$rand = gamipress_assoc_array_rand( $types ); |
| 226 |
if( is_array( $rand ) ) return $rand['plural_name']; |
| 227 |
|
| 228 |
return $default; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get a random post |
| 233 |
* |
| 234 |
* @since 1.0.0 |
| 235 |
* |
| 236 |
* @param array $post_types |
| 237 |
* |
| 238 |
* @return Object|false |
| 239 |
*/ |
| 240 |
function gamipress_ai_assistant_get_random_post( $post_types = array() ) { |
| 241 |
global $wpdb; |
| 242 |
|
| 243 |
$posts = GamiPress()->db->posts; |
| 244 |
|
| 245 |
$post_types_where = ''; |
| 246 |
|
| 247 |
if( count( $post_types ) === 1 ) { |
| 248 |
$post_type = sanitize_key( $post_types[0] ); |
| 249 |
$post_types_where = ' AND p.post_type = "' . $post_type . '"'; |
| 250 |
} else if( count( $post_types ) ) { |
| 251 |
$post_types_where = ' AND p.post_type IN ( "' . implode( '", "', $post_types ) . '" )'; |
| 252 |
} |
| 253 |
|
| 254 |
$post = $wpdb->get_row( "SELECT p.* |
| 255 |
FROM {$posts} AS p |
| 256 |
WHERE p.post_status IN ( 'publish', 'draft' ) |
| 257 |
{$post_types_where} |
| 258 |
ORDER BY rand() |
| 259 |
LIMIT 1" |
| 260 |
); |
| 261 |
|
| 262 |
return $post; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get a random plugin name |
| 267 |
* |
| 268 |
* @since 1.0.0 |
| 269 |
* |
| 270 |
* @return string |
| 271 |
*/ |
| 272 |
function gamipress_ai_assistant_get_random_plugin() { |
| 273 |
return gamipress_assoc_array_rand( gamipress_ai_assistant_get_available_plugins(), 'WordPress' ); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Get a list of random plugins names |
| 278 |
* |
| 279 |
* @since 1.0.0 |
| 280 |
* |
| 281 |
* @return string |
| 282 |
*/ |
| 283 |
function gamipress_ai_assistant_get_random_plugins( $desired = 3 ) { |
| 284 |
$available = gamipress_ai_assistant_get_available_plugins(); |
| 285 |
|
| 286 |
if( $desired === 1 ) |
| 287 |
return gamipress_ai_assistant_get_random_plugin(); |
| 288 |
|
| 289 |
|
| 290 |
if( count( $available ) < $desired ) |
| 291 |
return gamipress_join_words( $available ); |
| 292 |
|
| 293 |
$results = array(); |
| 294 |
|
| 295 |
for( $i = 0; $i < $desired; $i++ ) { |
| 296 |
$result = gamipress_assoc_array_rand( $available, 'WordPress' ); |
| 297 |
|
| 298 |
$k = array_search( $result, $available ); |
| 299 |
unset( $available[$k] ); |
| 300 |
|
| 301 |
$results[] = $result; |
| 302 |
} |
| 303 |
|
| 304 |
return gamipress_join_words( array_unique( $results ) ); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Get available plugins names |
| 309 |
* |
| 310 |
* @since 1.0.0 |
| 311 |
* |
| 312 |
* @return array |
| 313 |
*/ |
| 314 |
function gamipress_ai_assistant_get_available_plugins() { |
| 315 |
|
| 316 |
$options = array(); |
| 317 |
|
| 318 |
// if( class_exists( 'GamiPress' ) ) $options[] = 'GamiPress'; |
| 319 |
if( class_exists( 'AutomatorWP' ) ) $options[] = 'AutomatorWP'; |
| 320 |
if( class_exists( 'ShortLinksPro' ) ) $options[] = 'ShortLinks Pro'; |
| 321 |
if( class_exists( 'BBForms' ) ) $options[] = 'BBForms'; |
| 322 |
|
| 323 |
if( defined( 'BP_PLATFORM_VERSION' ) ) $options[] = 'BuddyBoss'; |
| 324 |
if( class_exists( 'BuddyPress' ) && ! defined( 'BP_PLATFORM_VERSION' ) ) $options[] = 'BuddyPress'; |
| 325 |
|
| 326 |
if( class_exists( 'SFWD_LMS' ) ) $options[] = 'LearnDash'; |
| 327 |
if( class_exists( 'LearnPress' ) ) $options[] = 'LearnPress'; |
| 328 |
if( class_exists( 'LifterLMS' ) ) $options[] = 'LifterLMS'; |
| 329 |
if( function_exists( 'tutor' ) ) $options[] = 'Tutor LMS'; |
| 330 |
if( class_exists( 'H5P_Plugin' ) ) $options[] = 'H5P'; |
| 331 |
|
| 332 |
if( class_exists( 'Easy_Digital_Downloads' ) ) $options[] = 'Easy Digital Downloads'; |
| 333 |
if( class_exists( 'SureCart' ) ) $options[] = 'SureCart'; |
| 334 |
if( class_exists( 'WooCommerce' ) ) $options[] = 'WooCommerce'; |
| 335 |
|
| 336 |
$options[] = 'WordPress'; |
| 337 |
|
| 338 |
return $options; |
| 339 |
} |