<?php
/**
 *
 * @package     productive-forms
 * @author      productiveminds.com
 * @copyright   productiveminds.com
 */

if ( !defined('ABSPATH') ) {
    die();
}

function productive_forms_register_meta_blog_element() {
    
    $args = array(
        'object_subtype' => PRODUCTIVE_FORMS_PLUGIN_BLOG_ELEMENT_POST_TYPE_SLUG,
        'single' => true,
        'type' => 'object',
        'show_in_rest' => array(
            'schema' => array(
                'type' => 'object',
                'properties' => array(
                    'cpt_video' => array(
                        'type' => 'string',
                    ),
                    'cpt_video_platform' => array(
                        'type' => 'string',
                    ),
                ),
                 'additionalProperties' => array(
                     'type' => 'string',
                 ),
            ),
        ),
        'sanitize_callback' => '',
        'auth_callback' => function() {
            return current_user_can( 'edit_posts' );
        }
    );
    register_meta( 'post', PRODUCTIVE_FORMS_PLUGIN_BLOG_ELEMENT_POST_TYPE_META_KEY, $args);
}
add_action( 'init', 'productive_forms_register_meta_blog_element' );


function productive_forms_pro_blog_element_add_meta_box() {
    $args = array(
        '__back_compat_meta_box' => true, // T
        '__block_editor_compatible_meta_box' => true,
    );
    add_meta_box('productive_pro_blog_element_id', __( 'Post Extra Fields', 'productive-forms' ), 'productive_forms_pro_blog_element_add_meta_box_callback', PRODUCTIVE_FORMS_PLUGIN_BLOG_ELEMENT_POST_TYPE_SLUG, 'normal', 'default', $args);
}
add_action( 'add_meta_boxes_' . PRODUCTIVE_FORMS_PLUGIN_BLOG_ELEMENT_POST_TYPE_SLUG, 'productive_forms_pro_blog_element_add_meta_box' );


function productive_forms_pro_blog_element_add_meta_box_callback( $post ) {
    $element_post_id = $post->ID;
    $element_meta_object = get_post_meta( $element_post_id, PRODUCTIVE_FORMS_PLUGIN_BLOG_ELEMENT_POST_TYPE_META_KEY, true );
    $cpt_video = '';
    if ( !empty( $element_meta_object['cpt_video'] ) ) {
        $cpt_video = $element_meta_object['cpt_video'];
    }
    $cpt_video_platform = '';
    if ( !empty( $element_meta_object['cpt_video_platform'] ) ) {
        $cpt_video_platform = $element_meta_object['cpt_video_platform'];
    }
    wp_nonce_field( 'elementnonce', '_pro_elementnonce' );
    ?>
    <table class="form-table">
        <tr>
            <th>
                <label for="pro_blog_element-cpt_video"><?php echo __( 'Video ID', 'productive-forms' ); ?></label>
                <label class="screen-reader-text" for="pro_blog_element-cpt_video"><?php echo __( 'Video ID', 'productive-forms' ); ?></label>
            </th>
            <td>
                <div><input id="pro_blog_element-cpt_video" class="regular-text" aria-required="true" type="text" value="<?php echo esc_attr( $cpt_video ); ?>" name="pro_blog_element[cpt_video]" class="form-required form-input-tip" /></div>
                <div><?php echo __( 'The video id of the video (not the full url, just the video ID)', 'productive-forms' ); ?></div>
            </td>
        </tr>
        <tr>
            <th>
                <label for="pro_blog_element-cpt_video_platform"><?php echo __( 'Video Platform', 'productive-forms' ); ?></label>
                <label for="pro_blog_element-cpt_video_platform" class="screen-reader-text" ><?php echo __( 'Video Platform', 'productive-forms' ); ?></label>
            </th>
            <td>
                <div>
                    <select id="pro_blog_element-cpt_video_platform" class="regular-select"
                                name="pro_blog_element[cpt_video_platform]">
                        <?php
                            $productive_global_get_video_platforms = productive_global_get_video_platforms();
                            foreach ( $productive_global_get_video_platforms as $key => $productive_forms_get_video_platform ) {
                                ?>
                                <option value="<?php echo esc_attr( $key ); ?>" <?php echo selected( $cpt_video_platform, esc_attr( $key ), false ); ?>>
                                   <?php echo esc_html( $productive_forms_get_video_platform ); ?>
                                </option>
                        <?php
                            }
                        ?>
                    </select>
                </div>
            </td>
        </tr>
    </table>
    <?php
}


function productive_forms_pro_blog_element_save_meta_box( $post_id, $post ) {
    $element_post_id = $post_id;
    
    if ( !isset( $_POST['_pro_elementnonce'] ) || !wp_verify_nonce( $_POST['_pro_elementnonce'], 'elementnonce' ) ) {
        return $element_post_id;
    }

    $element_post_type_object = get_post_type_object( $post->post_type );
    if ( !current_user_can( $element_post_type_object->cap->edit_post, $element_post_id ) ) {
        return $element_post_id;
    }
    
    if (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $element_post_id;
    }
    
    $this_posts_post_type = $post->post_type;
    if ( PRODUCTIVE_FORMS_PLUGIN_BLOG_ELEMENT_POST_TYPE_SLUG != $this_posts_post_type ) {
        return $element_post_id;
    }
    
    if ( isset( $_POST['pro_blog_element'] ) && ! empty( $_POST['pro_blog_element'] ) ) {
        $element_meta_object = array();
        if ( !empty( $_POST['pro_blog_element']['cpt_video'] ) ) {
            $element_meta_object['cpt_video'] = sanitize_text_field( $_POST['pro_blog_element']['cpt_video'] );
        }
        if ( !empty( $_POST['pro_blog_element']['cpt_video_platform'] ) ) {
            $element_meta_object['cpt_video_platform'] = sanitize_text_field( $_POST['pro_blog_element']['cpt_video_platform'] );
        }
        update_post_meta( $post_id, PRODUCTIVE_FORMS_PLUGIN_BLOG_ELEMENT_POST_TYPE_META_KEY, $element_meta_object);
    } else {
        delete_post_meta( $post_id, PRODUCTIVE_FORMS_PLUGIN_BLOG_ELEMENT_POST_TYPE_META_KEY);
    }
    
    return $element_post_id;
}
add_action( 'save_post', 'productive_forms_pro_blog_element_save_meta_box', 10, 20 );
