| 1 |
<?php |
| 2 |
/** |
| 3 |
* Requirements |
| 4 |
* |
| 5 |
* @package GamiPress\WP_Ulike\Requirements |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Add custom fields to the requirement object |
| 14 |
* |
| 15 |
* @param $requirement |
| 16 |
* @param $requirement_id |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
function gamipress_wp_ulike_requirement_object( $requirement, $requirement_id ) { |
| 21 |
|
| 22 |
if( isset( $requirement['trigger_type'] ) |
| 23 |
&& ( $requirement['trigger_type'] === 'gamipress_wp_ulike_post_type_like' |
| 24 |
|| $requirement['trigger_type'] === 'gamipress_wp_ulike_get_post_type_like' ) ) { |
| 25 |
|
| 26 |
// Post type |
| 27 |
$requirement['wp_ulike_post_type'] = get_post_meta( $requirement_id, '_gamipress_wp_ulike_post_type', true ); |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
return $requirement; |
| 32 |
} |
| 33 |
add_filter( 'gamipress_requirement_object', 'gamipress_wp_ulike_requirement_object', 10, 2 ); |
| 34 |
|
| 35 |
/** |
| 36 |
* Category field on requirements UI |
| 37 |
* |
| 38 |
* @param $requirement_id |
| 39 |
* @param $post_id |
| 40 |
*/ |
| 41 |
function gamipress_wp_ulike_requirement_ui_fields( $requirement_id, $post_id ) { |
| 42 |
|
| 43 |
$post_types = get_post_types( array( 'public' => true ), 'objects' ); |
| 44 |
$post_type = get_post_meta( $requirement_id, '_gamipress_wp_ulike_post_type', true ); |
| 45 |
?> |
| 46 |
|
| 47 |
<span class="wp-ulike-post-type"> |
| 48 |
<select> |
| 49 |
<?php foreach( $post_types as $key => $value ) : ?> |
| 50 |
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $post_type, $key ); ?>><?php echo $value->labels->singular_name; ?></option> |
| 51 |
<?php endforeach; ?> |
| 52 |
</select> |
| 53 |
</span> |
| 54 |
|
| 55 |
<?php |
| 56 |
} |
| 57 |
add_action( 'gamipress_requirement_ui_html_after_achievement_post', 'gamipress_wp_ulike_requirement_ui_fields', 10, 2 ); |
| 58 |
|
| 59 |
/** |
| 60 |
* Custom handler to save the custom fields on requirements UI |
| 61 |
* |
| 62 |
* @param $requirement_id |
| 63 |
* @param $requirement |
| 64 |
*/ |
| 65 |
function gamipress_wp_ulike_ajax_update_requirement( $requirement_id, $requirement ) { |
| 66 |
|
| 67 |
$requirement = wp_parse_args( $requirement, array( |
| 68 |
'wp_ulike_post_type' => '', |
| 69 |
) ); |
| 70 |
|
| 71 |
if( isset( $requirement['trigger_type'] ) |
| 72 |
&& ( $requirement['trigger_type'] === 'gamipress_wp_ulike_post_type_like' |
| 73 |
|| $requirement['trigger_type'] === 'gamipress_wp_ulike_get_post_type_like' ) ) { |
| 74 |
|
| 75 |
// Post type |
| 76 |
update_post_meta( $requirement_id, '_gamipress_wp_ulike_post_type', $requirement['wp_ulike_post_type'] ); |
| 77 |
|
| 78 |
} |
| 79 |
} |
| 80 |
add_action( 'gamipress_ajax_update_requirement', 'gamipress_wp_ulike_ajax_update_requirement', 10, 2 ); |