<?php
defined( "ABSPATH" ) || exit();
/**
 * Unlock module selection
 *
 * @param $modules
 *
 * @return mixed
 */
function wpcp_campaign_modules_list_unlock( $modules ) {
    foreach ( $modules as $key => $module ) {
        $modules[ $key ]['disabled'] = false;
    }

    return $modules;
}

add_filter( 'wpcp_campaign_modules_list', 'wpcp_campaign_modules_list_unlock' );


/**
 * Unlock all pro fields
 *
 * @since 1.2.0
 * @param $args
 *
 * @return mixed
 */
function wpcp_pro_unlock_input_field( $args ) {
    if ( isset( $args['wrapper_class'] ) && strpos($args['wrapper_class'], 'pro') !== false && isset( $args['attrs'] ) && array_key_exists( 'disabled', $args['attrs'] ) ) {
        unset( $args['attrs']['disabled'] );
        $args['wrapper_class'] = str_replace( 'pro', '', $args['wrapper_class'] );
    }

    return $args;
}

add_filter( 'wpcp_text_input', 'wpcp_pro_unlock_input_field' );
add_filter( 'wpcp_select_input', 'wpcp_pro_unlock_input_field' );
add_filter( 'wpcp_textarea_input', 'wpcp_pro_unlock_input_field' );
add_filter( 'wpcp_checkbox_input', 'wpcp_pro_unlock_input_field' );


function wpcp_pro_search_replace_metafield( $post ) {

    $search_replaces = wpcp_get_post_meta( $post->ID, '_wpcp_search_replaces', [] );
    ?>
    <div class="wpcp-repeater form-field wpcp-field _wpcp_custom_meta_fields-field pro">
        <label class="wpcp-label">Search Replace</label>

        <table class="striped widefat wp-list-table">
            <thead>
            <tr>
                <th>Search</th>
                <th>Replace</th>
                <th></th>
            </tr>
            </thead>
            <tbody data-repeater-list="_wpcp_search_replaces">
            <?php
            if ( ! empty( $search_replaces ) ):
                foreach ( $search_replaces as $search => $replace ) {
                    echo ' <tr data-repeater-item>';
                    echo sprintf( ' <td><input type="text" name="search" placeholder="Search" value="%s"/></td>', $search );
                    echo sprintf( ' <td><input type="text" name="replace" placeholder="Replace" value="%s"/></td>', $replace );
                    echo '<td><input data-repeater-delete type="button" value="Delete" class="button"/></td>';
                    echo '</tr>';
                }
            else: ?>
                <tr data-repeater-item>
                    <td><input type="text" name="search" placeholder="Search" value=""/></td>
                    <td><input type="text" name="replace" placeholder="Replace" value=""/></td>
                    <td><input data-repeater-delete type="button" value="Delete" class="button"/></td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>

        <button data-repeater-create type="button" class="button">Add New</button>
    </div>
    <?php
}

add_action( 'wpcp_advanced_settings_metabox', 'wpcp_pro_search_replace_metafield' );


function wpcp_pro_post_meta_metafield( $post ) {
    $post_metas = wpcp_get_post_meta( $post->ID, '_wpcp_custom_meta_fields', [] );
    ?>
    <div class="wpcp-repeater form-field wpcp-field _wpcp_custom_meta_fields-field pro">
        <label class="wpcp-label">Post Meta</label>

        <table class="striped widefat wp-list-table">
            <thead>
            <tr>
                <th>Meta Key</th>
                <th>Meta Value</th>
                <th></th>
            </tr>
            </thead>
            <tbody data-repeater-list="_wpcp_custom_meta_fields">

            <?php
            if ( ! empty( $post_metas ) ):
                foreach ( $post_metas as $meta_key => $meta_value ) {
                    echo ' <tr data-repeater-item>';
                    echo sprintf( ' <td><input type="text" name="meta_key" placeholder="Meta Key" value="%s"/></td>', $meta_key );
                    echo sprintf( ' <td><input type="text" name="meta_value" placeholder="Meta Value" value="%s"/></td>', $meta_value );
                    echo '<td><input data-repeater-delete type="button" value="Delete" class="button"/></td>';
                    echo '</tr>';
                }
            else: ?>
                <tr data-repeater-item>
                    <td><input type="text" name="meta_key" placeholder="Meta Key" value=""/></td>
                    <td><input type="text" name="meta_value" placeholder="Meta Value" value=""/></td>
                    <td><input data-repeater-delete type="button" value="Delete" class="button"/></td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>

        <button data-repeater-create type="button" class="button">Add New</button>
    </div>
    <?php
}

add_action( 'wpcp_advanced_settings_metabox', 'wpcp_pro_post_meta_metafield' );


function wpcp_pro_update_campaign_settings( $campaign_id, $posted ) {
    $excerpt_length = isset( $posted['_excerpt_length'])? intval($posted['_excerpt_length']): 55;
	update_post_meta( $campaign_id, '_excerpt_length', $excerpt_length );

    $custom_meta = array();
    if ( isset( $posted['_wpcp_custom_meta_fields'] ) && is_array( $posted['_wpcp_custom_meta_fields'] ) && count( $posted['_wpcp_custom_meta_fields'] ) ) {
        foreach ( $posted['_wpcp_custom_meta_fields'] as $field ) {
            $field = wp_parse_args( $field, array(
                'meta_key'   => '',
                'meta_value' => '',
            ) );
            $custom_meta[ sanitize_text_field( $field['meta_key'] ) ] = sanitize_text_field( $field['meta_value'] );
        }
    }
    update_post_meta( $campaign_id, '_wpcp_custom_meta_fields', $custom_meta );

    $search_replaces = array();
    if ( isset( $posted['_wpcp_search_replaces'] ) && is_array( $posted['_wpcp_search_replaces'] ) && count( $posted['_wpcp_search_replaces'] ) ) {
        foreach ( $posted['_wpcp_search_replaces'] as $field ) {
            $field  = wp_parse_args( $field, array(
                'search'   => '',
                'replace' => '',
            ) );
            $search_replaces[ sanitize_text_field( $field['search'] ) ] = sanitize_text_field( $field['replace'] );
        }
    }
    update_post_meta( $campaign_id, '_wpcp_search_replaces', $search_replaces );
}

add_action( 'wpcp_update_campaign_settings', 'wpcp_pro_update_campaign_settings', 10, 2 );


/**
 * @param $post_types
 *
 * @return mixed
 * @since 1.0.0
 *
 */
function wpcp_add_campaign_custom_post_types( $post_types ) {
    $args = array(
        'public'   => true,
        '_builtin' => false
    );

    $post_types_list = get_post_types( $args, 'names', 'and' );
    if ( ! empty( $post_types_list ) ) {
        foreach ( $post_types_list as $key => $post_type ) {
            $post_types[ $key ] = ucfirst( $post_type );
        }
    }

    return $post_types;
}

add_filter( 'wpcp_campaign_post_types', 'wpcp_add_campaign_custom_post_types' );

add_filter( 'wpcp_campaign_post_types', 'wpcp_add_campaign_custom_post_types' );




