| 1 |
<?php |
| 2 |
/** |
| 3 |
* Log Extra Data UI |
| 4 |
* |
| 5 |
* @package GamiPress\Admin\Log_Extra_Data_UI |
| 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 |
* Add log data and extra data meta box |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
function gamipress_add_log_extra_data_ui_meta_box() { |
| 19 |
|
| 20 |
add_meta_box( 'gamipress_log_details_ui', __( 'Log Details', 'gamipress' ), 'gamipress_log_details_ui_meta_box', 'gamipress_logs', 'side', 'default' ); |
| 21 |
add_meta_box( 'gamipress_log_extra_data_ui', __( 'Extra Data', 'gamipress' ), 'gamipress_log_extra_data_ui_meta_box', 'gamipress_logs', 'normal', 'default' ); |
| 22 |
|
| 23 |
} |
| 24 |
add_action( 'add_meta_boxes', 'gamipress_add_log_extra_data_ui_meta_box' ); |
| 25 |
|
| 26 |
/** |
| 27 |
* Renders the log data meta box |
| 28 |
* |
| 29 |
* @since 1.2.8 |
| 30 |
* |
| 31 |
* @param stdClass $object The current object |
| 32 |
*/ |
| 33 |
function gamipress_log_details_ui_meta_box( $object = null ) { |
| 34 |
?> |
| 35 |
<div id="log-data-ui"> |
| 36 |
<?php gamipress_log_details_ui_html( $object, $object->type ); ?> |
| 37 |
</div> |
| 38 |
<?php |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Renders the log data meta box HTML |
| 43 |
* |
| 44 |
* @since 1.2.8 |
| 45 |
* |
| 46 |
* @param stdClass $object The current object |
| 47 |
* @param string $type Type to render form |
| 48 |
*/ |
| 49 |
function gamipress_log_details_ui_html( $object, $type ) { |
| 50 |
|
| 51 |
global $ct_table; |
| 52 |
|
| 53 |
// Setup vars |
| 54 |
$log_types = gamipress_get_log_types(); |
| 55 |
$primary_key = $ct_table->db->primary_key; |
| 56 |
$object_id = $object->$primary_key; |
| 57 |
|
| 58 |
?> |
| 59 |
<div class="submitbox" id="submitpost" style="margin: -6px -12px -12px;"> |
| 60 |
|
| 61 |
<div id="minor-publishing"> |
| 62 |
|
| 63 |
<div id="misc-publishing-actions"> |
| 64 |
|
| 65 |
<div class="misc-pub-section misc-pub-post-status"> |
| 66 |
<?php echo __( 'Type:', 'gamipress' ); ?> <span id="post-status-display"><?php echo isset( $log_types[$object->type] ) ? $log_types[$object->type] : $object->type ; ?></span> |
| 67 |
</div> |
| 68 |
|
| 69 |
<div class="misc-pub-section misc-pub-visibility" id="visibility"> |
| 70 |
<?php echo __( 'Visibility:', 'gamipress' ); ?> <span id="post-visibility-display"><?php echo $object->access === 'public' ? __( 'Public', 'gamipress' ) : __( 'Private', 'gamipress' ); ?></span> |
| 71 |
</div> |
| 72 |
|
| 73 |
<div class="misc-pub-section curtime misc-pub-curtime"> |
| 74 |
<span id="timestamp"><?php echo __( 'Date:', 'gamipress' ); ?> <b><abbr title="<?php echo date( 'Y/m/d g:i:s a', strtotime( $object->date ) ); ?>"><?php echo date( 'Y/m/d', strtotime( $object->date ) ); ?></abbr></b></span> |
| 75 |
</div> |
| 76 |
|
| 77 |
</div> |
| 78 |
|
| 79 |
</div> |
| 80 |
|
| 81 |
<div id="major-publishing-actions"> |
| 82 |
|
| 83 |
<div id="delete-action"> |
| 84 |
<?php |
| 85 |
printf( |
| 86 |
'<a href="%s" class="submitdelete deletion" onclick="%s" aria-label="%s">%s</a>', |
| 87 |
ct_get_delete_link( $ct_table->name, $object_id ), |
| 88 |
"return confirm('" . |
| 89 |
esc_attr( __( "Are you sure you want to delete this item?\\n\\nClick \\'Cancel\\' to go back, \\'OK\\' to confirm the delete." ) ) . |
| 90 |
"');", |
| 91 |
esc_attr( __( 'Delete permanently' ) ), |
| 92 |
__( 'Delete Permanently' ) |
| 93 |
); |
| 94 |
?> |
| 95 |
</div> |
| 96 |
|
| 97 |
<div id="publishing-action"> |
| 98 |
<span class="spinner"></span> |
| 99 |
<?php submit_button( __( 'Save Changes' ), 'primary large', 'ct-save', false ); ?> |
| 100 |
</div> |
| 101 |
|
| 102 |
<div class="clear"></div> |
| 103 |
|
| 104 |
</div> |
| 105 |
|
| 106 |
</div> |
| 107 |
<?php |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Renders the log extra data |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* @param stdClass $object The current object |
| 115 |
*/ |
| 116 |
function gamipress_log_extra_data_ui_meta_box( $object = null ) { |
| 117 |
?> |
| 118 |
<div id="log-extra-data-ui"> |
| 119 |
<?php gamipress_log_extra_data_ui_html( $object, $object->log_id, $object->type ); ?> |
| 120 |
</div> |
| 121 |
<?php |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Renders the HTML for meta box based on the log type given |
| 126 |
* |
| 127 |
* @since 1.0.0 |
| 128 |
* |
| 129 |
* @param stdClass $object The current object |
| 130 |
* @param integer $object_id The current object ID |
| 131 |
* @param string $type Type to render form |
| 132 |
*/ |
| 133 |
function gamipress_log_extra_data_ui_html( $object, $object_id, $type ) { |
| 134 |
|
| 135 |
// Start with an underscore to hide fields from custom fields list |
| 136 |
$prefix = '_gamipress_'; |
| 137 |
$fields = array(); |
| 138 |
|
| 139 |
if( $type === 'event_trigger' ) { |
| 140 |
|
| 141 |
$fields = array( |
| 142 |
array( |
| 143 |
'name' => __( 'Trigger', 'gamipress' ), |
| 144 |
'tooltip' => __( 'The event user has triggered.', 'gamipress' ), |
| 145 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 146 |
'id' => 'trigger_type', |
| 147 |
'type' => 'advanced_select', |
| 148 |
'options' => gamipress_get_activity_triggers(), |
| 149 |
), |
| 150 |
array( |
| 151 |
'name' => __( 'Count', 'gamipress' ), |
| 152 |
'tooltip' => __( 'Number of times user triggered this event until this log.', 'gamipress' ), |
| 153 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 154 |
'id' => $prefix . 'count', |
| 155 |
'type' => 'text', |
| 156 |
), |
| 157 |
); |
| 158 |
|
| 159 |
$trigger = $object->trigger_type; |
| 160 |
|
| 161 |
// If is a specific activity trigger, then add the achievement_post field |
| 162 |
if( in_array( $trigger, array_keys( gamipress_get_specific_activity_triggers() ) ) ) { |
| 163 |
|
| 164 |
$achievement_post_id = ct_get_object_meta( $object_id, $prefix . 'achievement_post', true ); |
| 165 |
$achievement_post_site_id = ct_get_object_meta( $object_id, $prefix . 'achievement_post_site_id', true ); |
| 166 |
|
| 167 |
$fields[] = array( |
| 168 |
'name' => __( 'Assigned Post', 'gamipress' ), |
| 169 |
'tooltip' => __( 'Attached post to this log.', 'gamipress' ), |
| 170 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 171 |
'id' => $prefix . 'achievement_post', |
| 172 |
'type' => 'select', |
| 173 |
'options' => array( |
| 174 |
$achievement_post_id => gamipress_get_specific_activity_trigger_post_title( $achievement_post_id, $trigger, $achievement_post_site_id ), |
| 175 |
), |
| 176 |
); |
| 177 |
} |
| 178 |
} else if( $type === 'achievement_earn' || $type === 'achievement_award' ) { |
| 179 |
$achievement_id = ct_get_object_meta( $object_id, $prefix . 'achievement_id', true ); |
| 180 |
|
| 181 |
$fields = array( |
| 182 |
array( |
| 183 |
'name' => __( 'Achievement', 'gamipress' ), |
| 184 |
'tooltip' => __( 'Achievement user has earned.', 'gamipress' ), |
| 185 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 186 |
'id' => $prefix . 'achievement_id', |
| 187 |
'type' => 'select', |
| 188 |
'options' => array( |
| 189 |
$achievement_id => gamipress_get_post_field( 'post_title', $achievement_id ), |
| 190 |
), |
| 191 |
), |
| 192 |
); |
| 193 |
} else if( in_array( $type, array( 'points_award', 'points_earn', 'points_revoke', 'points_deduct', 'points_expend' ) ) ) { |
| 194 |
// Grab our points types as an array |
| 195 |
$points_types_options = array( |
| 196 |
'' => 'Default' |
| 197 |
); |
| 198 |
|
| 199 |
foreach( gamipress_get_points_types() as $slug => $data ) { |
| 200 |
$points_types_options[$slug] = $data['plural_name']; |
| 201 |
} |
| 202 |
|
| 203 |
$fields = array( |
| 204 |
array( |
| 205 |
'name' => __( 'Points', 'gamipress' ), |
| 206 |
'tooltip' => ( $type === 'points_award' || $type === 'points_earn' ? __( 'Points user has earned.', 'gamipress' ) : __( 'Points deducted to user.', 'gamipress' ) ), |
| 207 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 208 |
'id' => $prefix . 'points', |
| 209 |
'type' => 'text_small', |
| 210 |
), |
| 211 |
array( |
| 212 |
'name' => __( 'Points Type', 'gamipress' ), |
| 213 |
'tooltip' => ( $type === 'points_award' || $type === 'points_earn' ? __( 'Points type user has earned.', 'gamipress' ) : __( 'Points type deducted to user.', 'gamipress' ) ), |
| 214 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 215 |
'id' => $prefix . 'points_type', |
| 216 |
'type' => 'select', |
| 217 |
'options' => $points_types_options |
| 218 |
), |
| 219 |
array( |
| 220 |
'name' => __( 'Total Points', 'gamipress' ), |
| 221 |
'tooltip' => __( 'Total points user has earned until this log.', 'gamipress' ), |
| 222 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 223 |
'id' => $prefix . 'total_points', |
| 224 |
'type' => 'text_small', |
| 225 |
), |
| 226 |
); |
| 227 |
|
| 228 |
} else if( $type === 'rank_earn' || $type === 'rank_award' ) { |
| 229 |
$rank_id = ct_get_object_meta( $object_id, $prefix . 'rank_id', true ); |
| 230 |
|
| 231 |
$fields = array( |
| 232 |
array( |
| 233 |
'name' => __( 'Rank', 'gamipress' ), |
| 234 |
'tooltip' => __( 'Rank user has earned.', 'gamipress' ), |
| 235 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 236 |
'id' => $prefix . 'rank_id', |
| 237 |
'type' => 'select', |
| 238 |
'options' => array( |
| 239 |
$rank_id => gamipress_get_post_field( 'post_title', $rank_id ), |
| 240 |
), |
| 241 |
), |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
if( in_array( $type, array( 'achievement_award', 'points_award', 'points_revoke', 'rank_award' ) ) ) { |
| 246 |
$admin_id = ct_get_object_meta( $object_id, $prefix . 'admin_id', true ); |
| 247 |
$admin = get_userdata( $admin_id ); |
| 248 |
|
| 249 |
$fields[] = array( |
| 250 |
'name' => __( 'Administrator', 'gamipress' ), |
| 251 |
'tooltip' => ( $type === 'points_revoke' ? __( 'User has made the revoke.', 'gamipress' ) : __( 'User has made the award.', 'gamipress' ) ), |
| 252 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 253 |
'id' => $prefix . 'admin_id', |
| 254 |
'type' => 'select', |
| 255 |
'options' => array( |
| 256 |
$admin_id => $admin->user_login, |
| 257 |
), |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Log extra data fields |
| 263 |
* |
| 264 |
* @since 1.0.0 |
| 265 |
* @updated 1.6.3 Added $object parameter |
| 266 |
* |
| 267 |
* @param array $fields Log extra fields. |
| 268 |
* @param int $log_id The log ID. |
| 269 |
* @param string $type The log type. See gamipress_get_log_types(). |
| 270 |
* @param stdClass $object The log object. |
| 271 |
* |
| 272 |
* @return array |
| 273 |
*/ |
| 274 |
$fields = apply_filters( 'gamipress_log_extra_data_fields', $fields, $object_id, $type, $object ); |
| 275 |
|
| 276 |
if( ! empty( $fields ) ) { |
| 277 |
|
| 278 |
// Create a new box to render the form |
| 279 |
$cmb2 = new CMB2( array( |
| 280 |
'id' => 'log_extra_data_ui_box', |
| 281 |
'classes' => 'gamipress-form gamipress-box-form', |
| 282 |
'hookup' => false, |
| 283 |
'show_on' => array( |
| 284 |
'key' => 'gamipress_logs', |
| 285 |
'value' => $object_id |
| 286 |
), |
| 287 |
'fields' => $fields |
| 288 |
) ); |
| 289 |
|
| 290 |
$cmb2->object_id( $object_id ); |
| 291 |
|
| 292 |
$cmb2->show_form(); |
| 293 |
|
| 294 |
} else { |
| 295 |
|
| 296 |
_e( 'No extra data registered', 'gamipress' ); |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* AJAX Handler for retrieve the HTML with |
| 304 |
* |
| 305 |
* @since 1.0.0 |
| 306 |
* @return void |
| 307 |
*/ |
| 308 |
function gamipress_get_log_extra_data_ui_ajax_handler() { |
| 309 |
// Security check, forces to die if not security passed |
| 310 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 311 |
|
| 312 |
ct_setup_table( 'gamipress_logs' ); |
| 313 |
|
| 314 |
$ct_object = ct_get_object( absint( $_REQUEST['object_id'] ) ); |
| 315 |
|
| 316 |
gamipress_log_extra_data_ui_html( $ct_object, $ct_object->log_id, sanitize_text_field( $_REQUEST['type'] ) ); |
| 317 |
die; |
| 318 |
} |
| 319 |
add_action( 'wp_ajax_gamipress_get_log_extra_data_ui', 'gamipress_get_log_extra_data_ui_ajax_handler' ); |
| 320 |
|
| 321 |
/** |
| 322 |
* Helper function to recursive search an array key |
| 323 |
* |
| 324 |
* @since 1.0.0 |
| 325 |
* |
| 326 |
* @param $needle_key |
| 327 |
* @param $array |
| 328 |
* |
| 329 |
* @return bool|mixed |
| 330 |
*/ |
| 331 |
function gamipress_array_search_key( $needle_key, $array ) { |
| 332 |
|
| 333 |
foreach( $array as $key => $value ) { |
| 334 |
|
| 335 |
if( $key == $needle_key ) |
| 336 |
return $value; |
| 337 |
|
| 338 |
if( is_array( $value ) ) { |
| 339 |
if( ( $result = gamipress_array_search_key( $needle_key, $value ) ) !== false ) |
| 340 |
return $result; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
return false; |
| 345 |
|
| 346 |
} |