<?php
    /**
     * Create Admin Menu
     */
    require_once RENTPRESS_PLUGIN_DATA_ACCESS . 'data_layer.php';

    function rentpress_sync_options_page()
    {
        add_menu_page(
            'RentPress: Sync Settings',
            'RentPress',
            'manage_options',
            'rentpress',
            'rentpress_sync_options_page_html',
            RENTPRESS_PLUGIN_ADMIN_IMAGES_DIR . 'icon.png',
            RENTPRESS_MENU_POSITION
        );
    }
    add_action('admin_menu', 'rentpress_sync_options_page');

    /**
     * Do settings field wrapper
     */
    function add_settings_section_with_page($id, $title, $callback, $page, $args)
    {
        global $wp_settings_sections;

        $wp_settings_sections[ $page ][ $id ] = [
            'id'       => $id,
            'title'    => $title,
            'callback' => $callback,
            'args'     => $args,
        ];
    }

    function do_settings_fields_with_wrapper($page, $section)
    {
        // this function is for adding setting section fields with additional html that helps with making the settings page tabs. this function is based on the wordpress do_settings_fields function and is only used in the do_settings_sections_with_wrapper function
        global $wp_settings_fields;

        if (! isset($wp_settings_fields[ $page ][ $section ])) {
            return;
        }

        foreach ((array) $wp_settings_fields[ $page ][ $section ] as $field) {
            $class = '';

            if (! empty($field[ 'args' ][ 'class' ])) {
                $label = '';
                if (! empty($field[ 'args' ][ 'label_for' ])) {
                    $label = $field[ 'args' ][ 'label_for' ];
                }
                $class = ' id="' . $label . '" class="' . esc_attr($field[ 'args' ][ 'class' ]) . '"';
            }

            echo "<tr " . wp_kses_post($class) . ">";

            if (! empty($field[ 'args' ][ 'label_for' ])) {
                echo '<th scope="row"><label for="' . esc_attr($field[ 'args' ][ 'label_for' ]) . '">' . esc_html($field[ 'title' ]) . '</label></th>';
            } else {
                echo '<th scope="row">' . esc_html($field[ 'title' ]) . '</th>';
            }

            echo '<td>';
            if (function_exists($field[ 'callback' ])) {
                call_user_func($field[ 'callback' ], $field[ 'args' ]);
            }
            echo '</td>';
            echo '</tr>';
        }
    }

    /**
     * Do settings with wrapper
     */
    function do_settings_sections_with_wrapper($page)
    {
        // this function is for adding setting sections with additional html that helps with making the settings page tabs.
        // this function is based on the wordpress do_settings_sections function
        global $wp_settings_sections, $wp_settings_fields;

        if (! isset($wp_settings_sections[ $page ])) {
            return;
        }

        foreach ((array) $wp_settings_sections[ $page ] as $section) {
            $wrapperTitle = esc_attr(strtolower(str_replace(' ', '-', $section[ 'title' ])));
            $fieldPage    = '';
            if (! empty($section[ 'args' ][ 'setting_page' ]) && isset($section[ 'args' ][ 'setting_page' ])) {
                $fieldPage = 'data-page="' . esc_attr($section[ 'args' ][ 'setting_page' ]) . '"';
            }
            ?>
<div class="rentpress-settings-wrapper" id="rentpress-<?php echo esc_attr($wrapperTitle); ?>-settings-section"
    <?php echo wp_kses_data($fieldPage); ?>>
    <?php
            if ($section[ 'title' ]) {
                echo "<h2>" . esc_html($section[ 'title' ]) . "</h2>\n";
            }

            if ($section[ 'callback' ] && is_callable($section[ 'callback' ])) {
                call_user_func($section[ 'callback' ], $section);
            }

            echo '<table class="form-table">';
            do_settings_fields_with_wrapper($page, $section[ 'id' ]);
            echo '</table>';
            echo "</div>";

            if (! isset($wp_settings_fields) || ! isset($wp_settings_fields[ $page ]) || ! isset($wp_settings_fields[ $page ][ $section[ 'id' ] ])) {
                continue;
            }
        }
    }

    function getSitestimezone()
    {

        $tzstring = get_option('timezone_string');
        $offset   = get_option('gmt_offset');

        if (empty($tzstring) && 0 != $offset && floor($offset) == $offset) {
            $offset_st = $offset > 0 ? "-$offset" : '+' . absint($offset);
            $tzstring  = 'Etc/GMT' . $offset_st;
        }

        if (empty($tzstring)) {
            $tzstring = 'UTC';
        }

        $timezone = new DateTimeZone($tzstring);
        return $timezone;
    }

    /**
     * Create page view
     */
    function rentpress_sync_options_page_html()
    {

        // check user capabilities
        if (! current_user_can('manage_options')) {
            exit;
        }

        // check if the user have submitted the settings
        // wordpress will add the "settings-updated" $_GET parameter to the url
        if ( isset($_GET['settings-updated']) ) {
            add_settings_error('rentpress_messages', 'rentpress_message', __('Settings Saved', 'rentpress-for-websites'), 'updated');
        }

        // show error/update messages
        settings_errors('rentpress_messages');

        $options           = get_option('rentpress_options');
        $has_sync_token    = isset($options[ 'rentpress_api_credentials_section_api_token' ]) ? $options[ 'rentpress_api_credentials_section_api_token' ] : '';
        $has_sync_username = isset($options[ 'rentpress_api_credentials_section_username' ]) ? $options[ 'rentpress_api_credentials_section_username' ] : '';
        $has_sync_creds    = $has_sync_token && $has_sync_username;

    ?>
    <div id="rentpress-resync-loading-image" class="rentpress-pop-up-background" style="display: none;">
        <div></div>
        <img src="<?php echo esc_attr(RENTPRESS_PLUGIN_ADMIN_IMAGES_DIR) . '30l-square.png'; ?>"
            style="position: absolute;" class="rentpress-animate" alt="RentPress logo">
    </div>
    <section class="rentpress-settings-main">
        <div class="rentpress-settings-header">
            <img class="rentpress-settings-img" alt="RentPress logo"
                src="<?php echo esc_attr(RENTPRESS_PLUGIN_ADMIN_IMAGES_DIR) . '30l-square.png'; ?>">
            <h1 class="rentpress-settings-title">RentPress<span style="font-weight: 200; !important"> for
                    Websites</span>
            </h1>
            <p class="rentpress-version-number" style="color:Silver; padding-top:20px;">‎ ‎ version
                <?php echo esc_attr(RENTPRESS_PLUGIN_VERSION); ?></p>
        </div>
        <div class="rentpress-settings-controls">
            <aside class="rentpress-settings-menu-sticky">
                <div class="rentpress-tabs-menu">
                    <ul class="rentpress-tabs-menu-list" id="rentpress-tabs-menu-list">
                    </ul>
                    <div class="resync-options-container">
                        <input onclick="submitForm()" type="submit" name="submit" id="submit"
                            class="button button-primary rentpress-options-submit" value="Save Settings">
                    </div>
                </div>
            </aside>
            <div class="rentpress-admin-tabs-container">

                <div class="rentpress-admin-tab is-active-rentpress-admin-tab" id="rentpress_settings">
                    <h1><?php esc_html(get_admin_page_title()); ?></h1>
                    <h3 style="color: red">
                        <div id="results-for-sync-topline-call"></div>
                        <div id="results-for-marketing-sync"></div>
                        <div id="results-for-manual-sync"></div>
                        <div id="results-for-template-changes"></div>
                    </h3>

                    <form action="options.php" method="post" id="main-rentpress-settings">
                        <?php
              // output security fields for the registered setting "rentpress_options"
                  settings_fields('rentpress_settings');
                  // output setting sections and their fields
                  // (sections are registered for "rentpress", each field is registered to a specific section)
                  do_settings_sections_with_wrapper('rentpress_settings');
              ?>
                    </form>
                </div>

            </div>
        </div>
    </section>
    <?php
    }

    /**
     * Custom option and settings
     */
    function rentpress_settings_init()
    {
        // register a new setting for "rentpress_settings" page
        register_setting('rentpress_settings', 'rentpress_options');

        $rentpress_add_on_field_args = get_option('rentpress_add_on_field_args');
        $rentpress_add_on_field_args = $rentpress_add_on_field_args ? json_decode($rentpress_add_on_field_args) : false;

        if (isset($rentpress_add_on_field_args->rentpress_settings_sections) ? count((array) $rentpress_add_on_field_args->rentpress_settings_sections) : '') {
            foreach ($rentpress_add_on_field_args->rentpress_settings_sections as $settings_section_group) {
                foreach ($settings_section_group as $setting_section) {
                    add_settings_section_with_page($setting_section[ 0 ], $setting_section[ 1 ], $setting_section[ 2 ], $setting_section[ 3 ], is_array($setting_section[ 4 ]) ? $setting_section[ 4 ] : (array) $setting_section[ 4 ]);
                }
            }
        }

        if (isset($rentpress_add_on_field_args->rentpress_settings_fields) ? count((array) $rentpress_add_on_field_args->rentpress_settings_fields) : '') {
            foreach ($rentpress_add_on_field_args->rentpress_settings_fields as $settings_field_group) {
                foreach ($settings_field_group as $setting_field) {
                    add_settings_field($setting_field[ 0 ], $setting_field[ 1 ], $setting_field[ 2 ], $setting_field[ 3 ], $setting_field[ 4 ], is_array($setting_field[ 5 ]) ? $setting_field[ 5 ] : (array) $setting_field[ 5 ]);
                }
            }
        }

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_resync_submit_section',
            '',
            'rentpress_sync_options_page_html',
            'rentpress_resync',
            [
                'setting_page' => 'Data Sync',
             ]
        );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_api_credentials_section',
            'API Credentials',
            'rentpress_api_credentials_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Data Sync',
             ]
        );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_sync_log_section',
            'Sync Log',
            'rentpress_sync_log_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Data Sync',
             ]
        );

    //appearance jump section
        add_settings_section_with_page(
            'rentpress_appearance_jump_section',
            'Appearance',
            'rentpress_appearance_jump_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        //register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_accent_color_section',
            'Accent Color',
            'rentpress_accent_color_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_post_templates_section',
            'RentPress Templates',
            'rentpress_post_templates_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Enable Templates',
             ]
        );

        add_settings_field(
            'rentpress_post_templates_property_archive_section_input',
            'Property Search (Basic)',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_property_archive_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "A simple property search. Must be added to a page.",
                 ],
             ]
        );
        add_settings_field(
            'rentpress_post_templates_property_archive_advanced_section_input',
            'Property Search (Advanced)',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_property_archive_advanced_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Adds a map, per-bedroom pricing, and pet filters. Must be added to a page.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_property_single_section_input',
            'Property Single',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_property_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Listing for each property displaying info, floor plans, more. Appears automatically.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_floorplan_archive_section_input',
            'Floor Plan Search',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_floorplan_archive_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "A grid of all floor plans. Must be added to a page.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_floorplan_single_section_input',
            'Floor Plan Single',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_floorplan_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Product page for each floor plan displaying units, images, more. Appears automatically.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_city_archive_section_input',
            'Cities Grid',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_city_archive_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "A grid of all cities containing properties. Must be added to a page.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_city_single_section_input',
            'City Single',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_city_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Page for each city displaying properties, images, more. Appears automatically.",
                 ],
             ]
        );
        add_settings_field(
            'rentpress_post_templates_neighborhood_archive_section_input',
            'Neighborhoods Archive',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_neighborhood_archive_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "A grid of all neighborhoods containing properties. Must be added to a page.",
                 ],

             ]
        );
        add_settings_field(
            'rentpress_post_templates_neighborhood_single_section_input',
            'Neighborhood Page',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_neighborhood_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Page for each neighborhood displaying properties, images, more. Appears automatically.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_amenity_single_section_input',
            'Amenity Page',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_amenity_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Page for each amenity displaying properties, images, more. Appears automatically.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_feature_single_section_input',
            'Feature Page',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_feature_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Page for each feature displaying properties, images, more. Appears automatically.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_pet_single_section_input',
            'Pet Page',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_pet_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Page for each pet policy displaying properties, images, more. Appears automatically.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_property_type_single_section_input',
            'Property Type Page',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_property_type_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Page for each neighborhood displaying properties, images, more. Appears automatically.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_post_templates_property_taxonomy_single_section_input',
            'Property Taxonomies',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_post_templates_section',
            [
                'label_for'             => 'rentpress_post_templates_property_taxonomy_single_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Page for each of city, amenity, feature, pet, type. Appears automatically.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_accent_color_section_input', // what the field value will be saved as
            'Primary Accent Color',                 // Title
            'rentpress_accent_color_section_cb',    // function to generate html
            'rentpress_settings',                   // settings page that it is added to
            'rentpress_accent_color_section',       // what section the field will be added to
            [
                'label_for' => 'rentpress_accent_color_section_input',
                'class'     => 'rentpress_row',
                'default'   => '#3399ff',
             ]// any data that will be used in the callback function
        );

        add_settings_field(
            'rentpress_secondary_accent_color_section_input',
            'Secondary Color',
            'rentpress_accent_color_section_cb',
            'rentpress_settings',
            'rentpress_accent_color_section',
            [
                'label_for' => 'rentpress_secondary_accent_color_section_input',
                'class'     => 'rentpress_row',
                'default'   => '#0F1D4A',
             ]
        );

        // register a new field in the "rentpress_api_credentials_section" section, inside the "rentpress_settings" page
        add_settings_field(
            'rentpress_api_credentials_section_api_token',
            'License Key',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_api_credentials_section',
            [
                'label_for'             => 'rentpress_api_credentials_section_api_token',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => '',
                 ],
             ]
        );

        add_settings_field(
            'rentpress_api_credentials_section_username',
            'Company Username',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_api_credentials_section',
            [
                'label_for'             => 'rentpress_api_credentials_section_username',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => '',
                 ],
             ]
        );

        // sync btn section
        add_settings_field(
            'rentpress_api_credentials_section_sync_button',
            '',
            'rentpress_createSettingsSyncButton_cb',
            'rentpress_settings',
            'rentpress_api_credentials_section',
            [
                'label_for'             => 'rentpress_api_credentials_section_sync_button',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => '',
                 ],
             ]
        );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_unit_availability_section',
            'Unit Availability',
            'rentpress_unit_availability_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Feed Configuration',
             ]
        );

        add_settings_field(
            'rentpress_unit_availability_section_price_range_selector',
            'Price Ranges',
            'rentpress_createSettingsSelectorField_cb',
            'rentpress_settings',
            'rentpress_unit_availability_section',
            [
                'label_for'             => 'rentpress_unit_availability_section_price_range_selector',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => 'Select an option that will determine how prices are saved.',
                    'options'    => [
                        'Use Floor Plan Price If No Units Are Available',
                        'Use Floor Plan Price Only',
                        'Use Available Unit Price Only',
                     ],
                 ],
             ]
        );

        add_settings_field(
            'rentpress_unit_availability_section_lookahead',
            'Lookahead',
            'rentpress_createSettingsSelectorField_cb',
            'rentpress_settings',
            'rentpress_unit_availability_section',
            [
                'label_for'             => 'rentpress_unit_availability_section_lookahead',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => 'Select an option that will decide how far forward to look for available units.',
                    'options'    => [
                        '365 Days',
                        '180 Days',
                        '120 Days',
                        '90 Days',
                        '75 Days',
                        '60 Days',
                        '45 Days',
                        '30 Days',
                     ],
                 ],
             ]
        );

        add_settings_field(
            'rentpress_unit_availability_section_limit_unit_count',
            'Limit Number of Units saved to Floor Plans',
            'rentpress_createSettingsNumberField_cb',
            'rentpress_settings',
            'rentpress_unit_availability_section',
            [
                'label_for'             => 'rentpress_unit_availability_section_limit_unit_count',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => 'Enter a value greater than 1. Leave field empty to import all units from feed.',
                    'min'        => 1,
                 ],
             ]
        );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_floorplan_import_settings_section',
            'Floor Plan Import Settings',
            'rentpress_floorplan_import_settings_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Feed Configuration',
             ]
        );

        add_settings_field(
            'rentpress_prefix_floorplan_codes',
            'Make Floor Plan Codes Unique',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_floorplan_import_settings_section',
            [
                'label_for'             => 'rentpress_prefix_floorplan_codes',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Add property code prefix to floor plan codes.",
                 ],
             ]
        );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_floorplan_display_settings_section',
            'Floor Plan Display Settings',
            'rentpress_floorplan_display_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        add_settings_field(
            'rentpress_disable_floorplan_availability_counter',
            'Availability Counter',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_floorplan_display_settings_section',
            [
                'label_for'             => 'rentpress_disable_floorplan_availability_counter',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Hide unit counter on floor plan cards.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_hide_floorplans_with_no_availability',
            'Visibility by Available',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_floorplan_display_settings_section',
            [
                'label_for'             => 'rentpress_hide_floorplans_with_no_availability',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Hide floor plan cards with no available units.",
                 ],
             ]
        );

        add_settings_field(
            'rentpress_floorplan_title_display_section',
            'Floor Plan Title Type',
            'rentpress_createSettingsSelectorField_cb',
            'rentpress_settings',
            'rentpress_floorplan_display_settings_section',
            [
                'label_for'             => 'rentpress_floorplan_title_display_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => '<br />Floor plans can display with a marketing name or a consistent Bed | Bath type label.',
                    'options'    => [
                        'Marketing Name',
                        'bed | bath',
                     ],
                 ],
             ]
        );

        add_settings_field(
            'rentpress_apply_link_section',
            'Apply Link Target',
            'rentpress_createSettingsSelectorField_cb',
            'rentpress_settings',
            'rentpress_floorplan_display_settings_section',
            [
                'label_for'             => 'rentpress_apply_link_section',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => '<br />Select an option that will decide how the apply link opens.',
                    'options'    => [
                        'New Window',
                        'Same Window',
                     ],
                 ],
             ]
        );

        // add_settings_field(
        //     'rentpress_hide_waitlist_units',
        //     'Hide Waitlisted Units (Beta)',
        //     'rentpress_createSettingsCheckboxField_cb',
        //     'rentpress_settings',
        //     'rentpress_floorplan_display_settings_section',
        //     [
        //         'label_for'             => 'rentpress_hide_waitlist_units',
        //         'class'                 => 'rentpress_row',
        //         'rentpress_custom_data' => [
        //             'label_text' => "Hide waitlisted units (results may vary)",
        //          ],
        //      ]
        // );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_pricing_display_settings_section',
            'Pricing Display Settings',
            'rentpress_default_pricing_display_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        add_settings_field(
            'rentpress-pricing-display-settings-section_price_display_selection',
            'Default Rent Price Display Type',
            'rentpress_createSettingsRadioField_cb',
            'rentpress_settings',
            'rentpress_pricing_display_settings_section',
            [
                'label_for'             => 'rentpress_pricing_display_settings_section_price_display_selection',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'options' => [
                        'Base Rent',
                        'Market Rent',
                        'Effective Rent',
                        'Term Rent',
                        'Best Price',
                        'Minimum - Maximum',
                     ],
                 ],
                'default'               => 'Best Price',
             ]
        );

        // register a new field in the "rentpress_unit_rent_type_section" section, inside the "rentpress_settings" page

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_disable_pricing_section',
            'Hide Pricing',
            'rentpress_disable_pricing_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        // register a new field in the "rentpress_disable_pricing_section" section, inside the "rentpress_settings" page
        add_settings_field(
            'rentpress_disable_pricing_section_disable_pricing',
            'Hide Pricing',
            'rentpress_createSettingsCheckboxField_cb',
            'rentpress_settings',
            'rentpress_disable_pricing_section',
            [
                'label_for'             => 'rentpress_disable_pricing_section_disable_pricing',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => "Hide Pricing",
                 ],
             ]
        );

        add_settings_section_with_page(
            'rentpress_deposit_settings_section',
            'Deposit Settings',
            'rentpress_deposit_settings_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        add_settings_field(
            'rentpress_deposit_settings_section_deposit_settings',
            'Deposit Visibility',
            'rentpress_createSettingsSelectorField_cb',
            'rentpress_settings',
            'rentpress_deposit_settings_section',
            [
                'label_for'             => 'rentpress_deposit_settings_section_deposit_settings',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => '',
                    'options'    => [
                        'Always Show',
                        'Exclude $0 Deposits',
                        'Show None',
                     ],
                 ],
             ]
        );

        add_settings_field(
            'rentpress_disable_pricing_section_disable_pricing_message',
            'Price Placeholder Message',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_disable_pricing_section',
            [
                'label_for'             => 'rentpress_disable_pricing_section_disable_pricing_message',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'Default'          => 'Call For Pricing',
                    'placeholder_text' => 'ex: Call for Pricing',
                    'spellcheck'       => true,
                 ],
             ]
        );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_format_phone_numbers_section',
            'Format Phone Numbers',
            'rentpress_format_phone_numbers_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        // register a new field in the "rentpress_format_phone_numbers_section" section, inside the "rentpress_settings" page
        add_settings_field(
            'rentpress_format_phone_numbers_section_formatted_number',
            'Phone Format',
            'rentpress_createSettingsSelectorField_cb',
            'rentpress_settings',
            'rentpress_format_phone_numbers_section',
            [
                'label_for'             => 'rentpress_format_phone_numbers_section_formatted_number',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => '',
                    'options'    => [
                        '(xxx) xxx-xxxx',
                        '(xxx) xxx.xxxx',
                        '(xxx) xxx xxxx',
                        'xxx xxx xxxx',
                        'xxx.xxx.xxxx',
                        'xxx-xxx-xxxx',
                        'xxxxxxxxxx',
                     ],
                 ],
             ]
        );

    //integrations jump section
        add_settings_section_with_page(
            'rentpress_integrations_jump_section',
            'Integrations',
            'rentpress_integrations_jump_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Integrations',
             ]
        );

        // google analytics id
        add_settings_section_with_page(
            'rentpress_google_analytics_api_section',
            'Google Analytics',
            'rentpress_google_analytics_api_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Integrations',
             ]
        );

        // WILL BE REMOVED IN NEXT RELEASE
        add_settings_field(
            'rentpress_google_analytics_api_section_tracking_id',
            'GA4 Measurement ID',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_google_analytics_api_section',
            [
                'label_for'             => 'rentpress_google_analytics_api_section_tracking_id',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'placeholder_text' => 'ex: G-130303030-1',
                 ],
             ]
        );

        // google maps api key
        add_settings_section_with_page(
            'rentpress_google_maps_api_section',
            'Google Maps API',
            'rentpress_google_maps_api_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Integrations',
             ]
        );

        add_settings_field(
            'rentpress_google_maps_api_section_api_key',
            'Google API Key',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_google_maps_api_section',
            [
                'label_for'             => 'rentpress_google_maps_api_section_api_key',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'placeholder_text' => 'ex: AIzmSyBB2ND1X4K-LBkWS18uF2oKKinMINxFzWA',
                 ],
             ]
        );

        // Mapbox maps api key
        add_settings_section_with_page(
            'rentpress_mapbox_maps_api_section',
            'Mapbox API',
            'rentpress_mapbox_maps_api_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Integrations',
             ]
        );

        add_settings_field(
            'rentpress_mapbox_maps_api_section_api_key',
            'Mapbox API Key',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_mapbox_maps_api_section',
            [
                'label_for'             => 'rentpress_mapbox_maps_api_section_api_key',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'placeholder_text' => 'ex: AIzmSyBB2ND1X4K-LBkWS18uF2oKKinMINxFzWA',
                 ],
             ]
        );

        // Maps selector
        add_settings_section_with_page(
            'rentpress_map_source_api_section',
            'Template Map Source',
            'rentpress_map_source_api_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Integrations',
             ]
        );

        add_settings_field(
            'rentpress_map_source_api_section_selection',
            'Map Source',
            'rentpress_createSettingsSelectorField_cb',
            'rentpress_settings',
            'rentpress_map_source_api_section',
            [
                'label_for'             => 'rentpress_map_source_api_section_selection',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text' => '',
                    'options'    => [
                        'Google Maps',
                        'Mapbox',
                     ],
                 ],
             ]
        );

        // Amenities Add-on
        add_settings_section_with_page(
            'rentpress_amenities_manager_addon_section',
            'Amenities Manager Add-on',
            'rentpress_amenities_manager_addon_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Integrations',
             ]
        );

        add_settings_field(
            'rentpress_amenities_addon',
            'Amenities Manager ' . '‎ ‎ ‎ ‎  ' . 'Add-on',
            'rentpress_amenities_section_cb',
            'rentpress_settings',
            'rentpress_amenities_manager_addon_section',
            [
                'label_for' => 'rentpress_amenities_addon',
                'class'     => 'rentpress_row',
             ]
        );

        // Gravity Forms Add-on
        add_settings_section_with_page(
            'rentpress_gravity_forms_addon_section',
            'Gravity Forms Add-on',
            'rentpress_gravity_forms_addon_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Integrations',
             ]
        );

        add_settings_field(
            'rentpress_gravity_addon',
            'Gravity Forms ' . '‎ ‎ ‎ ‎ ‎  ' . 'Add-on',
            'rentpress_gravity_addon_section_cb',
            'rentpress_settings',
            'rentpress_gravity_forms_addon_section',
            [
                'label_for' => 'rentpress_gravity_addon',
                'class'     => 'rentpress_row',
             ]
        );

        // Map cluster image upload
        add_settings_section_with_page(
            'rentpress_cluster_image_section',
            'Placeholder Images',
            'rentpress_cluster_image_section_field_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        add_settings_field(
            'rentpress_default_property_image_section',
            'Default Property Image',
            'rentpress_image_upload_section_cb',
            'rentpress_settings',
            'rentpress_cluster_image_section',
            [
                'label_for' => 'rentpress_default_property_image_section',
                'class'     => 'rentpress_row',
                'default'   => RENTPRESS_PLUGIN_ADMIN_IMAGES_DIR . 'default_property_image.jpg',
             ]
        );

        add_settings_field(
            'rentpress_default_floor_plan_image_section',
            'Default Floor Plan Image',
            'rentpress_image_upload_section_cb',
            'rentpress_settings',
            'rentpress_cluster_image_section',
            [
                'label_for' => 'rentpress_default_floor_plan_image_section',
                'class'     => 'rentpress_row',
                'default'   => RENTPRESS_PLUGIN_ADMIN_IMAGES_DIR . 'default_floor_plan_image.png',
             ]
        );

        add_settings_field(
            'rentpress_default_city_image_section',
            'Default City Image',
            'rentpress_image_upload_section_cb',
            'rentpress_settings',
            'rentpress_cluster_image_section',
            [
                'label_for' => 'rentpress_default_city_image_section',
                'class'     => 'rentpress_row',
                'default'   => RENTPRESS_PLUGIN_ADMIN_IMAGES_DIR . 'default_city_image.jpg',
             ]
        );

        add_settings_field(
            'rentpress_cluster_image_section_input',
            'Custom Map Pin Cluster Marker',
            'rentpress_image_upload_section_cb',
            'rentpress_settings',
            'rentpress_cluster_image_section',
            [
                'label_for' => 'rentpress_cluster_image_section_input',
                'class'     => 'rentpress_row',
             ]
        );

        // register a new section in the "rentpress_settings" page
        add_settings_section_with_page(
            'rentpress_application_link_section',
            'Default URLs',
            'rentpress_application_link_section_cb',
            'rentpress_settings',
            [
                'setting_page' => 'Appearance',
             ]
        );

        // register a new field in the "rentpress_application_link_section" section, inside the "rentpress_settings" page
        add_settings_field(
            'rentpress_application_link_section_default_application_url',
            'Application URL',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_application_link_section',
            [
                'label_for'             => 'rentpress_application_link_section_default_application_url',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text'       => '',
                    'placeholder_text' => '',
                    'text_field_type'  => 'url',
                 ],
             ]
        );

        // register a new field in the "rentpress_application_link_section" section, inside the "rentpress_settings" page
        add_settings_field(
            'rentpress_application_link_section_url_contact',
            'Contact Page URL',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_application_link_section',
            [
                'label_for'             => 'rentpress_application_link_section_url_contact',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text'       => '',
                    'placeholder_text' => get_site_url() . '/contact/',
                    'default'          => get_site_url() . '/contact/',
                    'text_field_type'  => 'url',
                 ],
             ]
        );

        // register a new field in the "rentpress_application_link_section" section, inside the "rentpress_settings" page
        add_settings_field(
            'rentpress_application_link_section_url_tour',
            'Tour Page URL',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_application_link_section',
            [
                'label_for'             => 'rentpress_application_link_section_url_tour',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text'       => '',
                    'placeholder_text' => '',
                    'text_field_type'  => 'url',
                 ],
             ]
        );

        add_settings_field(
            'rentpress_application_link_section_url_waitlist',
            'Waitlist Page URL',
            'rentpress_createSettingsTextField_cb',
            'rentpress_settings',
            'rentpress_application_link_section',
            [
                'label_for'             => 'rentpress_application_link_section_url_waitlist',
                'class'                 => 'rentpress_row',
                'rentpress_custom_data' => [
                    'label_text'       => '',
                    'placeholder_text' => '',
                    'text_field_type'  => 'url',
                 ],
             ]
        );

    }
    add_action('admin_init', 'rentpress_settings_init');

    /**
     * Custom option and settings:
     * Callback functions
     * These are used for formating and displaying the options
     */

    function rentpress_resync_submit_section_cb($args)
    {
    ?>
    <p>Connected to Top Line Connect. You can resync pricing and availability information whenever you like.</p>
    <input id="rentpress-marketing-resync-button" class="rentpress-resync-button rentpress-settings-dark-btn"
        type="button" value="Resync Properties">
    <input id="rentpress-pricing-resync-button" class="rentpress-resync-button rentpress-settings-dark-btn"
        type="button" value="Resync Pricing">
    <?php
    // this form will fire off this hook when submitted
        add_action('admin_post_rentpress_resync_properties', 'rentpress_refresh_all_data_for_properties');
    }

    function rentpress_api_credentials_section_cb($args)
    {

    if ((get_option('rentpress_options')['rentpress_api_credentials_section_username'])
        && (get_option('rentpress_options')['rentpress_api_credentials_section_api_token'])
        && (get_option('rentpress_plugin_last_sync_attempt'))
     ) { ?>

    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php echo esc_attr('This website is connected via RentPress property data and will automatically resync every hour.')?>
        <?php echo esc_attr(' Click here to ');?><a href="https://via.30lines.com/d5VPJ3jz" target="_blank"
            rel="noopener noreferrer">update the subscription</a>.
        <br /><br />
        <strong>Need Help?</strong> Check the support site for assistance with RentPress: <a
            href="https://via.30lines.com/0F-Q2UnT">Get Started with RentPress</a>.
    </p>

    <?php } else { ?>

    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php echo esc_attr('Fill out these credentials to connect RentPress to property data. Contact 30 Lines to get a license key for this website. ')?><a
            href="https://via.30lines.com/vXP1UPFD" target="_blank" rel="noopener noreferrer">Purchase a
            subscription.</a>
        <br /><br />
        <?php echo esc_attr('Once connected, the website will automatically resync every hour.', 'rentpress-for-websites'); ?>
        <br /><br />
        <strong>Need Help?</strong> Check the support site for assistance setting up RentPress: <a
            href="https://via.30lines.com/0F-Q2UnT">Get Started</a>
    </p>

    <?php }

    }

    function rentpress_sync_log_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php echo esc_attr('RentPress will log when it has requested new data and when it has received new data.
            In most cases, these times should be within a few hours of each other.
            If the data feed does not see frequent activity, these times may occasionally drift farther apart.')?>
        <br /><br />
        <?php echo esc_attr('In most cases, if the times are more than a few hours apart, clicking the Sync Data button will get the site up to date.
            If issues persist, please ')?><a href="https://via.30lines.com/-ADerYil/">Contact Support</a>.
        <br /><br /><?php

                      global $wpdb;
                      // get the last sync date
                      $table_name      = $wpdb->prefix . 'rentpress_refresh';
                      $lastSync        = $wpdb->get_row("SELECT * FROM " . esc_sql($table_name));
                      $lastSyncAttempt = get_option('rentpress_plugin_last_sync_attempt');
                      $timezone        = getSitestimezone();

                      $date_format        = 'M j, Y';
                      $time_format        = get_option('time_format');
                      $timeFormat         = isset($date_format) && isset($time_format) ? $date_format . ' ' . $time_format : 'M j, Y H:i:s';
                      $lastSyncStr        = 'no sync data';
                      $lastSyncAttemptStr = 'no sync data';
                      if (isset($lastSync->last_refresh_time)) {
                          if ($timezone) {
                              $lastSyncStr = new DateTime();
                              $lastSyncStr = $lastSyncStr->setTimeStamp($lastSync->last_refresh_time)->setTimeZone($timezone)->format($timeFormat);
                          } else {
                              $lastSyncStr = '';
                          }
                      }
                      if ($lastSyncAttempt) {
                          $lastSyncAttemptStr = new DateTime();
                          $lastSyncAttemptStr = $lastSyncAttemptStr->setTimeStamp($lastSyncAttempt)->setTimeZone($timezone)->format($timeFormat);
                      }

                  if ($lastSyncAttemptStr != 'no sync data'): ?>
        <strong>Last Sync Request: </strong><?php echo esc_attr($lastSyncAttemptStr); ?><br />
        <?php endif;
      if ($lastSyncStr != 'no sync data'): ?>
        <strong>Received New Data: </strong><?php echo esc_attr($lastSyncStr); ?>
        <?php endif;
    ?>
    </p>
    <?php
    }

    function rentpress_unit_availability_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Use these options for pricing calculation. If no units are available, it will fall-back to default pricing ranges that come through the feed.', 'rentpress-for-websites'); ?>
    </p>
    <?php
    }

    function rentpress_floorplan_import_settings_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php echo wp_kses_post('
    Use this option when a data feed contains floor plans with duplicate floor plan codes, floor plans are missing, or other discrepancies are seen.
    <br/><br/>
    Once this option is enabled, visit the Data Sync settings page and complete <b>two successful data syncs</b>.
    For best results, this option should be set once and left alone.
    '); ?>
    </p>
    <?php
    }

    function rentpress_default_pricing_display_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Choose which type of rent to show by default. Each property can choose a unique price strategy in each property editor.', 'rentpress-for-websites'); ?>
    </p>
    <?php
    }

    function rentpress_floorplan_display_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Choose how floor plans display on this website.', 'rentpress-for-websites'); ?></p>
    <?php
    }

    function rentpress_disable_pricing_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Use these settings to remove pricing from displaying on this website. A placeholder message can show in place of pricing.', 'rentpress-for-websites'); ?>
    </p>
    <?php
    }

    function rentpress_deposit_settings_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Use this option to control the visibility of deposit values. All deposit valuess are shown by default. Deposits of $0 can be hidden. Or choose to hide deposits altogether. This can be overriden on a per-property basis in the Pricing settings for each property.', 'rentpress-for-websites'); ?>
    </p>
    <?php
    }

    function rentpress_post_templates_section_cb($args)
    {
        $options = get_option('rentpress_options');
        $fields  = [
            isset($options[ 'rentpress_post_templates_property_archive_section' ]) ? intval($options[ 'rentpress_post_templates_property_archive_section' ]) : '',
            isset($options[ 'rentpress_post_templates_property_archive_advanced_section' ]) ? intval($options[ 'rentpress_post_templates_property_archive_advanced_section' ]) : '',
            isset($options[ 'rentpress_post_templates_floorplan_archive_section' ]) ? intval($options[ 'rentpress_post_templates_floorplan_archive_section' ]) : '',
            isset($options[ 'rentpress_post_templates_property_single_section' ]) ? intval($options[ 'rentpress_post_templates_property_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_floorplan_single_section' ]) ? intval($options[ 'rentpress_post_templates_floorplan_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_city_single_section' ]) ? intval($options[ 'rentpress_post_templates_city_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_city_archive_section' ]) ? intval($options[ 'rentpress_post_templates_city_archive_section' ]) : '',
            isset($options[ 'rentpress_post_templates_amenity_single_section' ]) ? intval($options[ 'rentpress_post_templates_amenity_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_property_taxonomy_single_section' ]) ? intval($options[ 'rentpress_post_templates_property_taxonomy_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_amenity_single_section' ]) ? intval($options[ 'rentpress_post_templates_amenity_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_property_type_single_section' ]) ? intval($options[ 'rentpress_post_templates_property_type_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_feature_single_section' ]) ? intval($options[ 'rentpress_post_templates_feature_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_pet_single_section' ]) ? intval($options[ 'rentpress_post_templates_pet_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_neighborhood_single_section' ]) ? intval($options[ 'rentpress_post_templates_neighborhood_single_section' ]) : '',
            isset($options[ 'rentpress_post_templates_neighborhood_archive_section' ]) ? intval($options[ 'rentpress_post_templates_neighborhood_archive_section' ]) : '',
         ];
    ?>
    <?php if (! in_array(1, $fields)): ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e(
              'Click to choose templates to insert into the active theme, then click Save Changes.',
              'rentpress-for-websites'
      ); ?>
    </p>
    <?php endif; ?>
    <p><strong>Need Help? </strong>Check the support site for assistance setting up page templates: <a
            href="https://via.30lines.com/ZFHb1gFC" target="_blank" rel="noopener noreferrer">RentPress Settings: Enable
            Templates</a>.</p>
    <?php if (in_array(1, $fields)): ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        Click <strong>Add Theme Files</strong> to add the chosen templates into the active theme.
        Single templates will appear automatically for their respective posts.
        Archive, search, or grid templates must be applied to individual pages.
    </p>
    <input type="button" value="Add Theme Files" id="rentpress-theme-template-create"
        class="rentpress-settings-dark-btn">
    <br />
    <?php endif; ?>
    <?php
    }

    function rentpress_format_phone_numbers_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Select a default format for phone numbers.', 'rentpress-for-websites'); ?></p>
    <?php
    }

    function rentpress_application_link_section_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Use these fields to set default link targets for CTA buttons. Empty fields will not be used.', 'rentpress-for-websites'); ?>
    </p>
    <?php
    }

    function rentpress_accent_color_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Use these options to customize templates with brand colors. Most buttons and links use the Primary Accent Color. Each property can also choose unique brand colors.', 'rentpress-for-websites'); ?>
    </p>
    <?php
    }

    function rentpress_appearance_jump_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <a href="#rentpress_accent_color_section">Color</a> |
        <a href="#rentpress-floor-plan-display-settings-settings-section">Floor Plan Display</a> |
        <a href="#rentpress-pricing-display-settings-settings-section">Price Display</a> |
        <a href="#rentpress-hide-pricing-settings-section">Hide Pricing</a> |
        <a href="#rentpress-deposit-settings-settings-section">Deposits</a> |
        <a href="#rentpress-format-phone-numbers-settings-section">Phone Format</a> |
        <a href="#rentpress-placeholder-images-settings-section">Images</a> |
        <a href="#rentpress-default-urls-settings-section">Default URLs</a>
        </a>
        <br /><br />
        <strong>Need Help? </strong>Check the support site for assistance setting up page templates: <a
            href="https://via.30lines.com/tFezGUBS" target="_blank" rel="noopener noreferrer">RentPress Settings:
            Appearance</a>.
    </p>
    <?php
    }

    function rentpress_integrations_jump_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <a href="#rentpress_google_analytics_api_section">Analytics</a> |
        <a href="#rentpress-google-maps-api-settings-section">Google Maps</a> |
        <a href="#rentpress_mapbox_maps_api_section">Mapbox</a> |
        <a href="#rentpress_map_source_api_section">Map Source</a> |
        <a href="#rentpress-amenities-manager-add-on-settings-section">Amenities</a> |
        <a href="#rentpress-gravity-forms-add-on-settings-section">Forms</a>
        </a>
        <br /><br />
        <strong>Need Help? </strong>Check the support site for assistance with integrations: <a
            href="https://via.30lines.com/tFezGUBS" target="_blank" rel="noopener noreferrer">RentPress Settings:
            Integrations</a>.
    </p>
    <?php
    }

    function rentpress_google_maps_api_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        Enter a Google API key to use in RentPress templates. Retrieve the API key from the <a target="_blank"
            rel="noopener noreferrer" href="https://developers.google.com/maps/documentation/javascript/">Google Map
            Developer console</a>. <br /><br />
        This API key will be used to retrieve reviews when using the Reviews Add-On, and can be used to display maps on
        this site. More information can be found at: <a target="_blank" rel="noopener noreferrer"
            href="https://via.30lines.com/X7TllTnK">Understanding RentPress + Google Maps integration</a>.
    </p>
    <?php
    }

    function rentpress_mapbox_maps_api_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        Enter a Mapbox API key to use in RentPress templates.
    </p>
    <?php
    }

    function rentpress_map_source_api_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        Select which map to use in RentPress templates.
    </p>
    <?php
    }

    function rentpress_amenities_manager_addon_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        RentPress: Amenities Manager Add-on allows for fine tune the visibility and syncing of amenities from
        property management software.
        <br /><br />
        More information is available at <a href="https://via.30lines.com/fFXR3Ogq" target="_blank"
            rel="noopener noreferrer">RentPress: Amenities Manager Add-on documentation</a>.
    </p>
    <?php
    }

    function rentpress_gravity_forms_addon_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        RentPress: Gravity Forms Add-on connects contact forms with multifamily CRMs.
        <br /><br />
        More information about available shortcodes is available at <a href="https://via.30lines.com/xPdGhGjl"
            target="_blank" rel="noopener noreferrer">RentPress: Gravity Forms Add-on documentation</a>.
    </p>
    <?php
    }

    function rentpress_google_analytics_api_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">The templates included with
        RentPress
        will automatically report shopper clicks and actions to Google Analytics Tags found on the site. This works
        best
        when
        installed through Google Tag Manager.
        <br /><br />
        More information can be found at: <a target="_blank" rel="noopener noreferrer"
            href="https://via.30lines.com/WIoDyQiF">Understanding RentPress + Google Analytics integration</a>.
    </p>
    <?php
    }

    function rentpress_cluster_image_section_field_cb($args)
    {
    ?>
    <p id="<?php echo esc_attr($args[ 'id' ]); ?>" class="rentpress-settings-sub-title">
        <?php esc_html_e('Upload placeholder images for use in RentPress shortcodes and templates.', 'rentpress-for-websites'); ?>
    </p>
    <?php
    }

    function rentpress_createSettingsTextField_cb($args)
    {
        // get the value of the setting we've registered with register_setting()
        $options      = get_option('rentpress_options');
        $defaultValue = isset($args[ 'rentpress_custom_data' ][ 'default' ]) ? esc_attr($args[ 'rentpress_custom_data' ][ 'default' ]) : '';
        $needsValue   = $defaultValue ? $options[ $args[ 'label_for' ] ] : true;

        // output the field
    ?>
    <input
        type="<?php echo isset($args[ 'rentpress_custom_data' ][ 'text_field_type' ]) ? esc_attr($args[ 'rentpress_custom_data' ][ 'text_field_type' ]) : 'text' ?>"
        id="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="<?php echo esc_attr($args[ 'class' ]); ?> rentpress-text-field rentpress-settings-input" data-custom=""
        placeholder="<?php echo isset($args[ 'rentpress_custom_data' ][ 'placeholder_text' ]) ? esc_attr($args[ 'rentpress_custom_data' ][ 'placeholder_text' ]) : '' ?>"
        name="rentpress_options[<?php echo esc_attr($args[ 'label_for' ]); ?>]"
        value="<?php echo isset($options[ $args[ 'label_for' ] ]) && $needsValue ? esc_attr($options[ $args[ 'label_for' ] ]) : esc_attr($defaultValue) ?>">
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="rentpress-text-field-label rentpress-settings-label">
        <?php echo isset($args[ 'rentpress_custom_data' ][ 'label_text' ]) ? wp_kses_post($args[ 'rentpress_custom_data' ][ 'label_text' ]) : '' ?></label>
    <?php
    }

    function rentpress_createSettingsSyncButton_cb($args)
    {
        // get the value of the setting we've registered with register_setting()
        $options           = get_option('rentpress_options');
        $has_sync_token    = isset($options[ 'rentpress_api_credentials_section_api_token' ]) ? $options[ 'rentpress_api_credentials_section_api_token' ] : '';
        $has_sync_username = isset($options[ 'rentpress_api_credentials_section_username' ]) ? $options[ 'rentpress_api_credentials_section_username' ] : '';
        $has_sync_creds    = $has_sync_token && $has_sync_username;

        // output the button
    ?>
    <div class="rentpress-settings-wrapper" id="rentpress-marketing-resync-section" data-page="Data Sync"
        style="display: none;">
        <input id="rentpress-marketing-resync-button" class="rentpress-resync-button rentpress-settings-dark-btn"
            type="button" value="Sync Properties"
            style="<?php echo $has_sync_creds ? 'display: block;' : 'display: none;' ?>">
    </div>
    <?php
    }

    function rentpress_createSettingsNumberField_cb($args)
    {
        // get the value of the setting we've registered with register_setting()
        $options = get_option('rentpress_options');
        // output the field
    ?>
    <input type="number" id="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="<?php echo esc_attr($args[ 'class' ]); ?> rentpress-text-field rentpress-settings-input" data-custom=""
        max="<?php echo isset($args[ 'rentpress_custom_data' ][ 'max' ]) ? esc_attr($args[ 'rentpress_custom_data' ][ 'max' ]) : '' ?>"
        min="<?php echo isset($args[ 'rentpress_custom_data' ][ 'min' ]) ? esc_attr($args[ 'rentpress_custom_data' ][ 'min' ]) : '' ?>"
        placeholder="<?php echo isset($args[ 'rentpress_custom_data' ][ 'placeholder_text' ]) ? esc_attr($args[ 'rentpress_custom_data' ][ 'placeholder_text' ]) : '' ?>"
        name="rentpress_options[<?php echo esc_attr($args[ 'label_for' ]); ?>]"
        value="<?php echo isset($options[ $args[ 'label_for' ] ]) ? esc_attr($options[ $args[ 'label_for' ] ]) : '' ?>">
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="rentpress-text-field-label rentpress-settings-label">
        <?php echo isset($args[ 'rentpress_custom_data' ][ 'label_text' ]) ? esc_html($args[ 'rentpress_custom_data' ][ 'label_text' ]) : '' ?></label>
    <?php
    }

    function rentpress_createSettingsCheckboxField_cb($args)
    {
        // get the value of the setting we've registered with register_setting()
        $options = get_option('rentpress_options');
        // output the field
    ?>
    <input type="checkbox" id="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="<?php echo esc_attr($args[ 'class' ]); ?> rentpress-checkbox-field rentpress-settings-input"
        data-custom="" name="rentpress_options[<?php echo esc_attr($args[ 'label_for' ]); ?>]" value="1"
        <?php echo isset($options[ $args[ 'label_for' ] ]) ? (($options[ $args[ 'label_for' ] ] == '1') ? ' checked' : '') : '' ?>>
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="rentpress-checkbox-field-label rentpress-settings-label">
        <?php echo isset($args[ 'rentpress_custom_data' ][ 'label_text' ]) ? esc_html($args[ 'rentpress_custom_data' ][ 'label_text' ]) : '' ?></label>
    <?php
    }

    function rentpress_createSettingsSelectorField_cb($args)
    {
        $options                         = get_option('rentpress_options');
        $args[ 'rentpress_custom_data' ] = is_array($args[ 'rentpress_custom_data' ]) ? $args[ 'rentpress_custom_data' ] : (array) $args[ 'rentpress_custom_data' ];
    ?>
    <select id="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="<?php echo esc_attr($args[ 'class' ]); ?> rentpress-selector-field rentpress-settings-input"
        data-custom="" name="rentpress_options[<?php echo esc_attr($args[ 'label_for' ]); ?>]">
        <?php if (isset($args[ 'rentpress_custom_data' ][ 'options' ])):
          foreach ($args[ 'rentpress_custom_data' ][ 'options' ] as $opt): ?>
        <option value="<?php echo esc_attr($opt) ?>" class="rentpress-selector-field-option"
            <?php echo isset($options[ $args[ 'label_for' ] ]) ? (selected($options[ $args[ 'label_for' ] ], $opt, false)) : (''); ?>>
            <?php echo esc_html($opt, 'rentpress-for-websites'); ?>
        </option>
        <?php
          endforeach;
          endif; ?>
    </select>
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="rentpress-selector-field-label rentpress-settings-label">
        <?php echo isset($args[ 'rentpress_custom_data' ][ 'label_text' ]) ? wp_kses_post($args[ 'rentpress_custom_data' ][ 'label_text' ]) : '' ?>
    </label>
    <?php
    }

    function rentpress_createSettingsRadioField_cb($args)
    {
        $options = get_option('rentpress_options');
        if (isset($args[ 'rentpress_custom_data' ][ 'options' ])):
        foreach ($args[ 'rentpress_custom_data' ][ 'options' ] as $index => $opt): ?>
    <div class="rentpress-radio-field-wrapper">
        <input type="radio" id="<?php echo esc_attr($args[ 'label_for' ]) . esc_attr($index); ?>"
            class="rentpress-radio-field rentpress-settings-input"
            name="rentpress_options[<?php echo esc_attr($args[ 'label_for' ]); ?>]" data-custom=""
            value="<?php echo esc_attr($opt) ?>" <?php
        // if the value has not been set, then set the default value if it exists
            if (! isset($options[ $args[ 'label_for' ] ])) {
                echo isset($args[ 'default' ]) && $args[ 'default' ] == $opt ? ' checked' : '';
            } else {
                echo $options[ $args[ 'label_for' ] ] == $opt ? ' checked' : '';
        }
        ?>>
        <label for="<?php echo esc_attr($args[ 'label_for' ]) . esc_attr($index); ?>"
            class="rentpress-radio-field-label rentpress-settings-label"><?php echo esc_html($opt); ?></label><br>
    </div>
    <?php
        endforeach;
            endif;
        }

        function rentpress_accent_color_section_cb($args)
        {
            $options = get_option('rentpress_options');
            if (empty($options[ $args[ 'label_for' ] ])) {
                $options[ $args[ 'label_for' ] ] = $args[ 'default' ];
            }
            // output the field
        ?>
    <input type="color" id="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="<?php echo esc_attr($args[ 'label_for' ]); ?> rentpress-color-field rentpress-settings-input"
        data-custom="" name="rentpress_options[<?php echo esc_attr($args[ 'label_for' ]); ?>]"
        value="<?php echo esc_attr($options[ $args[ 'label_for' ] ]); ?>"
        <?php echo isset($options[ $args[ 'label_for' ] ]) ? (($options[ $args[ 'label_for' ] ] == '1') ? ' checked' : '') : '' ?>>
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="rentpress-checkbox-field-label rentpress-settings-label">
        <?php echo isset($args[ 'rentpress_custom_data' ][ 'title' ]) ? esc_html($args[ 'label_for' ]) : '' ?></label>

    <?php
    }

    function rentpress_gravity_addon_section_cb($args)
    {
        if (class_exists('rentPressGravityFormsLeads')):
            $gformAddonIsInstalled = "<p><b style='font-size:1.5em; color: #64C956;'>Installed</b><br/><br/>
				            <a href='/wp-admin/admin.php?page=gf_settings&subview=rentpress-gravity-forms'>Configure Lead Destinations →</a>
				            </p>";
        else:
            $gformAddonIsInstalled = "<p><b style='font-size:1.5em; color: #EC4D3E;'>Not Installed</b><br/><br/>
				            <a href='/wp-admin/plugin-install.php?s=rentpress&tab=search&type=term'>Download the plugin from WordPress →</a>
				            </p>";
    endif; ?>
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>" class="rentpress-settings-label">
        <?php echo wp_kses_post($gformAddonIsInstalled); ?></label>

    <?php
    }

    function rentpress_amenities_section_cb($args)
    {
        if (function_exists('rentpress_add_amenities_manager_settings')):
            $amenityAddonIsInstalled = "<p><b style='font-size:1.5em; color: #64C956;'>Installed</b></p>";
        else:
            $amenityAddonIsInstalled = "<p><b style='font-size:1.5em; color: #EC4D3E;'>Not Installed</b><br/><br/>
				            <a href='/wp-admin/plugin-install.php?s=rentpress&tab=search&type=term'>Download the plugin from WordPress →</a>
				            </p>";
    endif; ?>
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>" class="rentpress-settings-label">
        <?php echo wp_kses_post($amenityAddonIsInstalled); ?></label>
    <?php
    }

    function rentpress_cluster_image_section_cb($args)
    {
        $options = get_option('rentpress_options');
        // output the field
    ?>
    <input id="rentpress-cluster-image-upload" value="<?php echo esc_attr($options[ $args[ 'label_for' ] ]) ?? ''; ?>"
        type="text" name="rentpress_options[<?php echo esc_attr($args[ 'label_for' ]); ?>]">
    <button class="button wpse-228085-upload">Upload</button>
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="rentpress-checkbox-field-label rentpress-settings-label">
        <?php echo isset($args[ 'rentpress_custom_data' ][ 'title' ]) ? esc_html($args[ 'label_for' ]) : '' ?></label>
    <?php if (isset($options[ $args[ 'label_for' ] ]) && $options[ $args[ 'label_for' ] ] != ''): ?>
    <div>
        <img id="rentpress-custom-cluster-pin-image" class="rentpress-custom-cluster-pin-image"
            alt="Cluster of items on the map" src="<?php echo esc_attr($options[ $args[ 'label_for' ] ]) ?? ''; ?>">
    </div>
    <?php endif; ?>
    <?php
    }

    function rentpress_image_upload_section_cb($args)
    {
        $options      = get_option('rentpress_options');
        $defaultValue = isset($args[ 'default' ]) ? $args[ 'default' ] : '';
        $needsValue   = $defaultValue ? $options[ $args[ 'label_for' ] ] : true;
        // output the field
    ?>
    <input id="<?php echo esc_attr($args[ 'label_for' ]); ?>-field"
        value="<?php echo isset($options[ $args[ 'label_for' ] ]) && $needsValue ? esc_attr($options[ $args[ 'label_for' ] ]) : esc_attr($defaultValue) ?>"
        type="text" name="rentpress_options[<?php echo esc_attr($args[ 'label_for' ]); ?>]">
    <span
        class="button rentpress-image-uploader-field                                                   <?php echo esc_attr($args[ 'label_for' ]); ?>"
        data-target="<?php echo esc_attr($args[ 'label_for' ]); ?>" data-limit='true'>Upload</span>
    <label for="<?php echo esc_attr($args[ 'label_for' ]); ?>"
        class="rentpress-checkbox-field-label rentpress-settings-label">
        <?php echo isset($args[ 'rentpress_custom_data' ][ 'title' ]) ? esc_html($args[ 'label_for' ]) : '' ?></label>
    <?php if (isset($options[ $args[ 'label_for' ] ]) && $options[ $args[ 'label_for' ] ] != ''): ?>
    <div id="<?php echo esc_attr($args[ 'label_for' ]); ?>-upload-preview-container"
        class="<?php echo esc_attr($args[ 'label_for' ]); ?>-image-wrapper">
        <img id="<?php echo esc_attr($args[ 'label_for' ]); ?>-image"
            class="<?php echo esc_attr($args[ 'label_for' ]); ?>-image rentpress-image-upload-preview"
            alt="Image upload preview" src="<?php echo esc_attr($options[ $args[ 'label_for' ] ]) ?? ''; ?>">
    </div>
    <?php else: ?>
    <div style="display: none;" id="<?php echo esc_attr($args[ 'label_for' ]); ?>-image-wrapper"
        class="<?php echo esc_attr($args[ 'label_for' ]); ?>-image-wrapper">
        <img alt="Image upload preview" id="<?php echo esc_attr($args[ 'label_for' ]); ?>-image"
            class="<?php echo esc_attr($args[ 'label_for' ]); ?>-image rentpress-image-upload-preview">
    </div>
    <?php endif;
    }
    /**
     * Resync Hooks
     */

    add_action('admin_enqueue_scripts', function () {
        wp_enqueue_media();
    });

    add_action('wp_ajax_rentpress_getAllPricingDataForProperties', 'rentpress_getAllPricingDataForProperties');
    add_action('wp_ajax_rentpress_createThemeTemplateFile', 'rentpress_createThemeTemplateFile');

    function rentpress_getAllPricingDataForProperties()
    {
        require_once RENTPRESS_PLUGIN_DATA_SYNC . 'property/pricing_refresh.php';
        rentpress_syncFeedAndWPProperties();
        wp_die(); // this is required to terminate immediately and return a proper response
    }

    function rentpress_createThemeTemplateFile()
    {
        $options            = get_option('rentpress_options');
        $templates          = [  ];
        $usePropTaxTemplate = isset($options[ 'rentpress_post_templates_property_taxonomy_single_section' ]) ? intval($options[ 'rentpress_post_templates_property_taxonomy_single_section' ]) : '';
        if ($usePropTaxTemplate) {
            $templates[  ] = 'taxonomy.php';
        }
        if (isset($options[ 'rentpress_post_templates_property_archive_section' ]) ? intval($options[ 'rentpress_post_templates_property_archive_section' ]) : '') {
            $templates[  ] = 'archive-rentpress_property.php';
        }
        if (isset($options[ 'rentpress_post_templates_property_archive_advanced_section' ]) ? intval($options[ 'rentpress_post_templates_property_archive_advanced_section' ]) : '') {
            $templates[  ] = 'archive-rentpress_property-advanced.php';
        }
        if (isset($options[ 'rentpress_post_templates_floorplan_archive_section' ]) ? intval($options[ 'rentpress_post_templates_floorplan_archive_section' ]) : '') {
            $templates[  ] = 'archive-rentpress_floorplan.php';
        }
        if (isset($options[ 'rentpress_post_templates_property_single_section' ]) ? intval($options[ 'rentpress_post_templates_property_single_section' ]) : '') {
            $templates[  ] = 'single-rentpress_property.php';
        }
        if (isset($options[ 'rentpress_post_templates_floorplan_single_section' ]) ? intval($options[ 'rentpress_post_templates_floorplan_single_section' ]) : '') {
            $templates[  ] = 'single-rentpress_floorplan.php';
        }
        if (isset($options[ 'rentpress_post_templates_city_single_section' ]) ? intval($options[ 'rentpress_post_templates_city_single_section' ]) : '' && ! $usePropTaxTemplate) {
            $templates[  ] = 'taxonomy-city.php';
        }
        if (isset($options[ 'rentpress_post_templates_city_archive_section' ]) ? intval($options[ 'rentpress_post_templates_city_archive_section' ]) : '') {
            $templates[  ] = 'template-city-archive.php';
        }
        if (isset($options[ 'rentpress_post_templates_amenity_single_section' ]) ? intval($options[ 'rentpress_post_templates_amenity_single_section' ]) : '' && ! $usePropTaxTemplate) {
            $templates[  ] = 'taxonomy-amenity.php';
        }
        if (isset($options[ 'rentpress_post_templates_pet_single_section' ]) ? intval($options[ 'rentpress_post_templates_pet_single_section' ]) : '' && ! $usePropTaxTemplate) {
            $templates[  ] = 'taxonomy-pet.php';
        }
        if (isset($options[ 'rentpress_post_templates_feature_single_section' ]) ? intval($options[ 'rentpress_post_templates_feature_single_section' ]) : '' && ! $usePropTaxTemplate) {
            $templates[  ] = 'taxonomy-feature.php';
        }
        if (isset($options[ 'rentpress_post_templates_property_type_single_section' ]) ? intval($options[ 'rentpress_post_templates_property_type_single_section' ]) : '' && ! $usePropTaxTemplate) {
            $templates[  ] = 'taxonomy-property_type.php';
        }
        if (isset($options[ 'rentpress_post_templates_neighborhood_single_section' ]) ? intval($options[ 'rentpress_post_templates_neighborhood_single_section' ]) : '') {
            $templates[  ] = 'single-rentpress_hood.php';
        }
        if (isset($options[ 'rentpress_post_templates_neighborhood_archive_section' ]) ? intval($options[ 'rentpress_post_templates_neighborhood_archive_section' ]) : '') {
            $templates[  ] = 'template-neighborhood-archive.php';
        }

        foreach ($templates as $key => $template) {
            if ($template) {
                $theme  = get_stylesheet_directory() . '/' . $template;
                $plugin = RENTPRESS_PLUGIN_DIR . 'public/templates/' . $template;

                if ($template == 'taxonomy.php' && file_exists($theme) && ! file_exists(get_template_directory() . '/original_taxonomy.php')) {
                    $rename = rename($theme, get_template_directory() . '/original_taxonomy.php');
                }
                if (! copy($plugin, $theme)) {
                    $errors = error_get_last();
                    echo esc_attr('error: ' . $errors[ 'message' ] . ' ' . $template);
                } else {
                    echo esc_attr('Success: ' . $template . ' has been created ');
                }
            }
        }
    }

    function rentpress_updateFloorplanCodes($propertyCodes, $addParentCode)
    {
        require_once RENTPRESS_PLUGIN_DATA_ACCESS . 'data_layer.php';
        require_once RENTPRESS_PLUGIN_DATA_MODEL . 'refresh_model.php';
        require_once RENTPRESS_PLUGIN_DATA_MODEL . 'floorplan_model.php';

        // set option value so plugin knows where in the process it is
        if ($addParentCode) {
            update_option("rentpress_floorplan_code_upgrade_status", 1);
        } else {
            update_option("rentpress_floorplan_code_upgrade_status", 0);
        }
        // for each property add or remove the property code to its child floorplans
        foreach ($propertyCodes as $code) {
            $propertyData = rentpress_getAllPropertyDataWithCodeOrPostID($code);
            // check for floorplans
            if (isset($propertyData->floorplans) ? count($propertyData->floorplans) : "") {
                $floorplans = $propertyData->floorplans;
                foreach ($floorplans as $floorplan) {
                    // check if this is an upgrade or downgrade
                    if ($addParentCode) {
                        // merge codes before setting them
                        $newCode = $propertyData->property_code . "_" . $floorplan->floorplan_code;
                        // check for codes that are already prefixed by TLC or plugin bugs
                        if (! str_contains($floorplan->floorplan_code, $propertyData->property_code . "_")) {
                            // override needs set so sync doent override change
                            update_post_meta($floorplan->floorplan_post_id, "rentpress_custom_field_floorplan_code_override", "on");
                            update_post_meta($floorplan->floorplan_post_id, "rentpress_custom_field_floorplan_code", $newCode);
                        }
                    } else {
                        $oldCode = str_replace($propertyData->property_code . "_", "", $floorplan->floorplan_code);
                        // override needs set so sync doent override change
                        update_post_meta($floorplan->floorplan_post_id, "rentpress_custom_field_floorplan_code_override", "on");
                        update_post_meta($floorplan->floorplan_post_id, "rentpress_custom_field_floorplan_code", $oldCode);
                    }
                }
            }
        }
        rentpress_emptyFloorplanTable();
        rentpress_deleteAllRefreshData();
        wp_schedule_single_event(time(), 'rentpress_cron_hook_sync_data');
    }

    $rentpress_options             = get_option('rentpress_options');
    $floorplan_code_upgrade_status = intval(get_option("rentpress_floorplan_code_upgrade_status"));
    $floorplan_prefix_setting      = isset($rentpress_options[ 'rentpress_prefix_floorplan_codes' ])
    ? $rentpress_options[ 'rentpress_prefix_floorplan_codes' ]
    : "";
    $floorplanNeedsRevert = $floorplan_code_upgrade_status && ! $floorplan_prefix_setting;
    $floorplaNeedsUpgrade = ! $floorplan_code_upgrade_status && $floorplan_prefix_setting;

    if ($floorplanNeedsRevert || $floorplaNeedsUpgrade) {
        $properties    = rentpress_getAllProperties();
        $propertyCodes = [];
        foreach ($properties as $key => $property) {
            $propertyCodes[  ] = $property->property_code;
        }
        rentpress_updateFloorplanCodes($propertyCodes, $floorplaNeedsUpgrade);
    }

?>