require_once dirname(__FILE__) . '/classes/class-ccf-form-renderer.php';
require_once dirname(__FILE__) . '/classes/class-ccf-form-handler.php';
require_once dirname(__FILE__) . '/classes/class-ccf-upgrader.php';
require_once dirname(__FILE__) . '/classes/class-ccf-widget.php';
require_once dirname(__FILE__) . '/classes/class-ccf-export.php';
require_once dirname(__FILE__) . '/classes/class-ccf-ads.php';
require_once dirname(__FILE__) . '/classes/class-ccf-settings.php';
CCF_Custom_Contact_Forms::factory();
CCF_Constants::factory();
CCF_Form_CPT::factory();
CCF_Submission_CPT::factory();
CCF_Field_CPT::factory();
CCF_Choice_CPT::factory();
CCF_Form_Manager::factory();
CCF_Form_Renderer::factory();
CCF_Field_Renderer::factory();
CCF_Form_Handler::factory();
CCF_Upgrader::factory();
CCF_Export::factory();
CCF_Ads::factory();
CCF_Settings::factory();
/**
 * Setup the widget
 *
 * @since 6.4
 */
function ccf_register_widget()
{
    register_widget('CCF_Widget');
}
add_action('widgets_init', 'ccf_register_widget');
    /**
     * Return form HTML for a given form ID
     *
     * @param int $form_id
     * @since 6.0
     * @return string
     */
    public function get_rendered_form($form_id)
    {
        $form = get_post((int) $form_id);
        if (!$form) {
            return '';
        }
        $fields = get_post_meta($form_id, 'ccf_attached_fields', true);
        $pause = get_post_meta($form_id, 'ccf_form_pause', true);
        if (empty($fields)) {
            return '';
        }
        ob_start();
        if (!empty($pause)) {
            $pause_message = get_post_meta($form_id, 'ccf_form_pause_message', true);
            ?>

			<div class="ccf-form-pause form-id-<?php 
            echo (int) $form_id;
            ?>
">
				<?php 
            if (empty($pause_message)) {
                ?>
					<?php 
                esc_html_e('This form is paused right now. Check back later!', 'custom-contact-forms');
                ?>
				<?php 
            } else {
                ?>
					<?php 
                echo esc_html($pause_message);
                ?>
				<?php 
            }
            ?>
			</div>

			<?php 
        } elseif (!empty($_POST['ccf_form']) && !empty($_POST['form_id']) && $_POST['form_id'] == $form_id && empty(CCF_Form_Handler::factory()->errors_by_form[$form_id])) {
            $completion_message = get_post_meta($form_id, 'ccf_form_completion_message', true);
            ?>

			<div class="ccf-form-complete form-id-<?php 
            echo (int) $form_id;
            ?>
">
				<?php 
            if (empty($completion_message)) {
                ?>
					<?php 
                esc_html_e('Thank you for your submission.', 'custom-contact-forms');
                ?>
				<?php 
            } else {
                ?>
					<?php 
                echo esc_html($completion_message);
                ?>
				<?php 
            }
            ?>
			</div>

			<?php 
        } else {
            $contains_file = false;
            $fields_html = '';
            $conditionals = array();
            foreach ($fields as $field_id) {
                $field_id = (int) $field_id;
                $type = esc_attr(get_post_meta($field_id, 'ccf_field_type', true));
                $slug = get_post_meta($field_id, 'ccf_field_slug', true);
                $conditionals_enabled = get_post_meta($field_id, 'ccf_field_conditionalsEnabled', true);
                if (!empty($conditionals_enabled)) {
                    // Todo: escaping?
                    $field_conditionals = get_post_meta($field_id, 'ccf_attached_conditionals', true);
                    if (!empty($field_conditionals)) {
                        $new_conditionals = array('conditions' => $field_conditionals, 'conditionalType' => get_post_meta($field_id, 'ccf_field_conditionalType', true), 'conditionalFieldsRequired' => get_post_meta($field_id, 'ccf_field_conditionalFieldsRequired', true));
                        $conditionals[$slug] = $new_conditionals;
                    }
                }
                if ('file' === $type) {
                    $contains_file = true;
                }
                $fields_html .= apply_filters('ccf_field_html', CCF_Field_Renderer::factory()->render_router($type, $field_id, $form_id), $type, $field_id);
            }
            if (CCF_Field_Renderer::factory()->section_open) {
                $fields_html .= '</div>';
            }
            $theme = get_post_meta($form_id, 'ccf_form_theme', true);
            if (empty($theme)) {
                $theme = 'default';
            }
            ?>

			<div class="ccf-form-wrapper form-id-<?php 
            echo (int) $form_id;
            ?>
" data-form-id="<?php 
            echo (int) $form_id;
            ?>
">
				<form <?php 
            if ($contains_file) {
                ?>
enctype="multipart/form-data"<?php 
            }
            ?>
 class="ccf-form ccf-theme-<?php 
            echo esc_attr($theme);
            ?>
" method="post" action="" data-form-id="<?php 
            echo (int) $form_id;
            ?>
">

					<?php 
            $title = get_the_title($form_id);
            if (!empty($title) && apply_filters('ccf_show_form_title', true, $form_id)) {
                ?>
						<div class="form-title">
							<?php 
                echo $title;
                ?>
						</div>
					<?php 
            }
            ?>

					<?php 
            $description = get_post_meta($form_id, 'ccf_form_description', true);
            if (!empty($description) && apply_filters('ccf_show_form_description', true, $form_id)) {
                ?>
						<div class="form-description">
							<?php 
                echo esc_html($description);
                ?>
						</div>
					<?php 
            }
            ?>

					<?php 
            echo $fields_html;
            ?>

					<div class="form-submit">
						<input type="submit" class="btn btn-primary ccf-submit-button" value="<?php 
            echo esc_attr(get_post_meta($form_id, 'ccf_form_buttonText', true));
            ?>
">
						<img class="loading-img" src="<?php 
            echo esc_url(site_url('/wp-admin/images/wpspin_light.gif'));
            ?>
">
					</div>

					<script type="text/javascript">
					window.wp = window.wp || {};
					wp.ccf = wp.ccf || {};
					wp.ccf.conditionals = wp.ccf.conditionals || [];
					wp.ccf.conditionals[<?php 
            echo (int) $form_id;
            ?>
] = <?php 
            echo wp_json_encode($conditionals);
            ?>
;
					</script>

					<input type="hidden" name="form_id" value="<?php 
            echo (int) $form_id;
            ?>
">
					<input type="hidden" name="form_page" value="<?php 
            echo esc_url(untrailingslashit(site_url()) . $_SERVER['REQUEST_URI']);
            ?>
">
					<input type="text" name="my_information" style="display: none;">
					<input type="hidden"  name="ccf_form" value="1">
					<input type="hidden" name="form_nonce" value="<?php 
            echo wp_create_nonce('ccf_form');
            ?>
">
				</form>

				<iframe class="ccf-form-frame" id="ccf_form_frame_<?php 
            echo (int) $form_id;
            ?>
" name="ccf_form_frame_<?php 
            echo (int) $form_id;
            ?>
"></iframe>
			</div>

			<?php 
        }
        $form_html = ob_get_clean();
        return $form_html;
    }
    /**
     * Return form HTML for a given form ID
     *
     * @param int $form_id
     * @since 6.0
     * @return string
     */
    public function get_rendered_form($form_id)
    {
        $form = get_post((int) $form_id);
        if (!$form) {
            return '';
        }
        $fields = get_post_meta($form_id, 'ccf_attached_fields', true);
        $pause = get_post_meta($form_id, 'ccf_form_pause', true);
        if (empty($fields)) {
            return '';
        }
        ob_start();
        if (!empty($pause)) {
            $pause_message = get_post_meta($form_id, 'ccf_form_pause_message', true);
            ?>

			<div class="ccf-form-pause form-id-<?php 
            echo (int) $form_id;
            ?>
">
				<?php 
            if (empty($pause_message)) {
                ?>
					<?php 
                esc_html_e('This form is paused right now. Check back later!', 'custom-contact-forms');
                ?>
				<?php 
            } else {
                ?>
					<?php 
                echo esc_html($pause_message);
                ?>
				<?php 
            }
            ?>
			</div>

			<?php 
        } elseif (!empty($_POST['ccf_form']) && !empty($_POST['form_id']) && $_POST['form_id'] == $form_id && empty(CCF_Form_Handler::factory()->errors_by_form[$form_id])) {
            $completion_message = get_post_meta($form_id, 'ccf_form_completion_message', true);
            ?>

			<div class="ccf-form-complete form-id-<?php 
            echo (int) $form_id;
            ?>
">
				<?php 
            if (empty($completion_message)) {
                ?>
					<?php 
                esc_html_e('Thank you for your submission.', 'custom-contact-forms');
                ?>
				<?php 
            } else {
                ?>
					<?php 
                echo esc_html($completion_message);
                ?>
				<?php 
            }
            ?>
			</div>

			<?php 
        } else {
            $contains_file = false;
            $fields_html = '';
            foreach ($fields as $field_id) {
                $field_id = (int) $field_id;
                $type = esc_attr(get_post_meta($field_id, 'ccf_field_type', true));
                if ('file' === $type) {
                    $contains_file = true;
                }
                $fields_html .= apply_filters('ccf_field_html', CCF_Field_Renderer::factory()->render_router($type, $field_id, $form_id), $type, $field_id);
            }
            ?>

			<div class="ccf-form-wrapper form-id-<?php 
            echo (int) $form_id;
            ?>
" data-form-id="<?php 
            echo (int) $form_id;
            ?>
">
				<form <?php 
            if ($contains_file) {
                ?>
enctype="multipart/form-data"<?php 
            }
            ?>
 class="ccf-form" method="post" action="" data-form-id="<?php 
            echo (int) $form_id;
            ?>
">

					<?php 
            $title = get_the_title($form_id);
            if (!empty($title) && apply_filters('ccf_show_form_title', true, $form_id)) {
                ?>
						<div class="form-title">
							<?php 
                echo $title;
                ?>
						</div>
					<?php 
            }
            ?>

					<?php 
            $description = get_post_meta($form_id, 'ccf_form_description', true);
            if (!empty($description) && apply_filters('ccf_show_form_description', true, $form_id)) {
                ?>
						<div class="form-description">
							<?php 
                echo esc_html($description);
                ?>
						</div>
					<?php 
            }
            ?>

					<?php 
            echo $fields_html;
            ?>

					<div class="form-submit">
						<input type="submit" class="ccf-submit-button" value="<?php 
            echo esc_attr(get_post_meta($form_id, 'ccf_form_buttonText', true));
            ?>
">
						<img class="loading-img" src="<?php 
            echo esc_url(site_url('/wp-admin/images/wpspin_light.gif'));
            ?>
">
					</div>

					<input type="hidden" name="form_id" value="<?php 
            echo (int) $form_id;
            ?>
">
					<input type="hidden" name="form_page" value="<?php 
            echo esc_url(untrailingslashit(site_url()) . $_SERVER['REQUEST_URI']);
            ?>
">
					<input type="text" name="my_information" style="display: none;">
					<input type="hidden"  name="ccf_form" value="1">
					<input type="hidden" name="form_nonce" value="<?php 
            echo wp_create_nonce('ccf_form');
            ?>
">
				</form>

				<iframe onload="wp.ccf.iframeOnload( <?php 
            echo (int) $form_id;
            ?>
 );" class="ccf-form-frame" id="ccf_form_frame_<?php 
            echo (int) $form_id;
            ?>
" name="ccf_form_frame_<?php 
            echo (int) $form_id;
            ?>
"></iframe>
			</div>

			<?php 
        }
        $form_html = ob_get_clean();
        return $form_html;
    }