<?php

/* Menu */

add_action ( 'admin_menu', 'wpfresh5_add_admin_menu' );

function wpfresh5_add_admin_menu() {
    require_once( WPFRESH5_DIR.'/lib/styles/styles.php');
    // ask_delete_license();
    // delete_site_option( 'as_style1' );
    $style1 = ask12_lic_status();
    // $style1 = true;
    $page1 = add_menu_page( 'WP Freshstart 5', 'WP Freshstart 5', 'manage_options', 'wp-freshstart5', ($style1 ? 'wpfresh5_screen_main' : 'ask12_settings'), WPFRESH5_URL.'/img/wp-fresh-icon.png' );
    if( $style1 ) {
        $page2 = add_submenu_page( 'wp-freshstart5', 'Content', 'Content', 'manage_options', 'wp-freshstart5-content', 'wpfresh5_screen_content' );
        $page11 = add_submenu_page( 'wp-freshstart5', 'Legal', 'Legal', 'manage_options', 'wp-freshstart5-legal', 'wpfresh5_screen_legal' );
        $page12 = add_submenu_page( 'wp-freshstart5', 'GDPR', 'GDPR', 'manage_options', 'wp-freshstart5-gdpr', 'wpfresh5_screen_gdpr' );
        $page3 = add_submenu_page( 'wp-freshstart5', 'Plugins', 'Plugins', 'manage_options', 'wp-freshstart5-plugins', 'wpfresh5_screen_plugins' );
        $page4 = add_submenu_page( 'wp-freshstart5', 'Security', 'Security', 'manage_options', 'wp-freshstart5-security', 'wpfresh5_screen_security' );
        $page5 = add_submenu_page( 'wp-freshstart5', 'SEO', 'SEO', 'manage_options', 'wp-freshstart5-seo', 'wpfresh5_screen_seo' );
        if( wpfresh5_is_pro() ) {
            $page6 = add_submenu_page( 'wp-freshstart5', 'Branding', 'Branding', 'manage_options', 'wp-freshstart5-branding', 'wpfresh5_screen_branding' );
            $page7 = add_submenu_page( 'wp-freshstart5', 'Social', 'Social', 'manage_options', 'wp-freshstart5-social', 'wpfresh5_screen_social' );
        }
        $page13 = add_submenu_page( 'wp-freshstart5', 'Monetize', 'Monetize', 'manage_options', 'wp-freshstart5-monetize', 'wpfresh5_screen_monetize' );
        $page8 = add_submenu_page( 'wp-freshstart5', 'Others', 'Others', 'manage_options', 'wp-freshstart5-others', 'wpfresh5_screen_others' );
        $page9 = add_submenu_page( 'wp-freshstart5', 'InstaReset', 'InstaReset', 'manage_options', 'wp-freshstart5-reset', 'wpfresh5_screen_reset' );
    }
        $page10 = add_submenu_page( 'wp-freshstart5', 'Help', 'Help', 'manage_options', 'wp-freshstart5-support', 'wpfresh5_screen_help' );
        // remove_submenu_page( 'wp-freshstart', 'wp-freshstart' );

    if( $style1 ) {
        add_action( "admin_print_scripts-$page2", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page2", 'wpfresh5_admin_style' );
        add_action( "admin_print_styles-$page2", 'wpfresh5_admin_style_fonts' );
        add_action( "admin_print_scripts-$page3", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page3", 'wpfresh5_admin_style' );
        add_action( "admin_print_scripts-$page4", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page4", 'wpfresh5_admin_style' );
        add_action( "admin_print_scripts-$page5", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page5", 'wpfresh5_admin_style' );
        if( wpfresh5_is_pro() ) {
            add_action( "admin_print_scripts-$page6", 'wpfresh5_admin_scripts' );
            add_action( "admin_print_styles-$page6", 'wpfresh5_admin_style' );
            add_action( "admin_print_scripts-$page7", 'wpfresh5_admin_scripts' );
            add_action( "admin_print_styles-$page7", 'wpfresh5_admin_style' );
        }
        add_action( "admin_print_scripts-$page8", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page8", 'wpfresh5_admin_style' );
        add_action( "admin_print_scripts-$page9", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page9", 'wpfresh5_admin_style' );

        add_action( "admin_print_scripts-$page11", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page11", 'wpfresh5_admin_style' );
        add_action( "admin_print_scripts-$page12", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page12", 'wpfresh5_admin_style' );
        add_action( "admin_print_scripts-$page13", 'wpfresh5_admin_scripts' );
        add_action( "admin_print_styles-$page13", 'wpfresh5_admin_style' );
    }
    add_action( "admin_print_scripts-$page1", 'wpfresh5_admin_scripts' );
    add_action( "admin_print_styles-$page1", 'wpfresh5_admin_style' );
    add_action( "admin_print_scripts-$page10", 'wpfresh5_admin_scripts' );
    add_action( "admin_print_styles-$page10", 'wpfresh5_admin_style' );
}

function wpfresh5_admin_scripts() {
    wp_enqueue_script('wp-color-picker');
    wp_enqueue_style( 'wp-color-picker' );
    wp_enqueue_media( );
	wp_register_script( 'wpfresh5_admin_js', WPFRESH5_URL . '/js/wpfresh5-admin.js', array( 'jquery' ), WPFRESH5_VERSION);

    $script_vars = array(
		'admin_url' => admin_url( 'admin.php?page=wp-freshstart5' )
	 );
	wp_localize_script( 'wpfresh5_admin_js', 'wpfresh5_vars', $script_vars );
	wp_enqueue_script( 'wpfresh5_admin_js' );

    wp_register_script( 'wpfresh5_popoverjs', WPFRESH5_URL . '/js/jquery.webui-popover.min.js', array('jquery'), WPFRESH5_VERSION);
    wp_enqueue_script( 'wpfresh5_popoverjs');

	wp_register_script( 'wpfresh5-formalize',
	        WPFRESH5_URL . '/js/formalize.js',
	        array( 'jquery' ) );
	wp_enqueue_script( 'wpfresh5-formalize' );

}

function wpfresh5_admin_style() {
	wp_register_style( 'wpfresh5_font_awesome_css', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', array(), WPFRESH5_VERSION);
	wp_enqueue_style( 'wpfresh5_font_awesome_css' );

    wp_register_style('wpfresh5_popover', WPFRESH5_URL . '/css/jquery.webui-popover.min.css');
    wp_enqueue_style( 'wpfresh5_popover' );

	wp_register_style( 'wpfresh5_custom_css', WPFRESH5_URL . '/css/wpfresh5-admin.css', array(), WPFRESH5_VERSION);
	wp_enqueue_style( 'wpfresh5_custom_css' );
}

function wpfresh5_admin_style_fonts() {
    $enable_fonts = wpfresh5_get_settings( array( 'content', 'fonts', 'enable_fonts' ) );
    if( !$enable_fonts )
        return;
    $fonts_txt = '';
    for( $i=1; $i<6;$i++ ) {
        $font = wpfresh5_get_settings( array( 'content', 'fonts', 'font'.$i ) );
        if( $font )
            $fonts_txt .= str_replace( ' ', '+', $font).'|';
    }
    wp_register_style('wpfresh5_google_fonts', WPFRESH5_GOOGLE_FONTS_URL.'/css?family='.$fonts_txt);
    wp_enqueue_style( 'wpfresh5_google_fonts');
}

function wpfresh5_cpt_enqueue( $hook_suffix ){
    if( in_array($hook_suffix, array('post.php', 'post-new.php') ) ){
        $screen = get_current_screen();
        if( is_object( $screen ) ){
            wpfresh5_admin_style_fonts();
        }
    }
}

add_action( 'admin_enqueue_scripts', 'wpfresh5_cpt_enqueue');

function wpfresh5_screen_main() {
	if ( !current_user_can ( 'manage_options' ))
		wp_die ( 'You don\'t have access to this page.' );
	if ( !user_can_access_admin_page ())
		wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
	<div class="wrap">
		<img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <?php if( isset( $_GET['quickw'] ) ) {
            echo '<h1>WP Freshstart - Quick Settings - <a href="https://wpfreshstart5.com/support/knowledgebase/how-to-use-wp-freshstart-quick-settings-wizard/" target="_blank" style="font-size:0.8em">Support</a> <a href="#" id="wpfresh5-expand-all" class="button button-secondary" style="float:right"><i class="fa fa-expand"></i> Expand all</a></h1><br/>';
            wpfresh5_quick_wizard();
            echo '<br/></div>';
            return;
        }
        else {
        ?>
        <h1>WP Freshstart <?php if( wpfresh5_is_pro() ) echo 'Pro';?> - <a href="https://wpfreshstart5.com/support/knowledgebase/how-to-get-started/" target="_blank" style="font-size:0.8em">Support</a></h1>
        <br/>
        <p>WP Freshstart 5.0 helps you save precious minutes (sometimes hours!) when setting up your WordPress site</p>
        <p><a href="<?php echo admin_url('admin.php?page=wp-freshstart5');?>&quickw=true" class="button button-primary"><i class="fa fa-lightbulb-o"></i> Click here to use our Quick Settings</a><br/><br/>or use any of the advanced settings below:</p>
        <!-- <p>Here are the different settings you can configure:</p> -->
        <p>

        <?php
            if( wpfresh5_is_pro() ) {
                $els = array(
                    array(
                        'id' => 'content',
                        'header' => 'Content',
                        'desc' => 'Create content automatically'
                    ),
                    array(
                        'id' => 'legal',
                        'header' => 'Legal',
                        'desc' => 'Handles all the legal stuff (cookies, legal pages, etc)'
                    ),
                    array(
                        'id' => 'gdpr',
                        'header' => 'GDPR',
                        'desc' => 'Everything you need to comply with the EU GDPR Law'
                    ),
                    array(
                        'id' => 'plugins',
                        'header' => 'Plugins',
                        'desc' => 'Install plugins with one click'
                    ),
                    array(
                        'id' => 'security',
                        'header' => 'Security',
                        'desc' => 'Avoid getting your content stolen, and your website hacked'
                    ),
                    array(
                        'id' => 'seo',
                        'header' => 'SEO',
                        'desc' => 'Take action to improve your rankings in Search Engines'
                    ),
                    array(
                        'id' => 'branding',
                        'header' => 'Branding',
                        'desc' => 'Change the look and feel of the WordPress admin area with your own settings (ideal for your clients)'
                    ),
                    array(
                        'id' => 'social',
                        'header' => 'Social',
                        'desc' => 'Add social buttons, metadata, or share your content'
                    ),
                    array(
                        'id' => 'monetize',
                        'header' => 'Monetize',
                        'desc' => 'Monetize your content on autopilot'
                    ),
                    array(
                        'id' => 'others',
                        'header' => 'Others',
                        'desc' => 'Other settings (optimize, footer/header code, email...)'
                    ),
                    array(
                        'id' => 'reset',
                        'header' => 'InstaReset',
                        'desc' => 'Reset all database tables (handle with care!)'
                    ),
                );
            }
            else {
                $els = array(
                    array(
                        'id' => 'content',
                        'header' => 'Content',
                        'desc' => 'Create content automatically'
                    ),
                    array(
                        'id' => 'legal',
                        'header' => 'Legal',
                        'desc' => 'Handles all the legal stuff (cookies, legal pages, etc)'
                    ),
                    array(
                        'id' => 'gdpr',
                        'header' => 'GDPR',
                        'desc' => 'Everything you need to comply with the EU GDPR Law'
                    ),
                    array(
                        'id' => 'plugins',
                        'header' => 'Plugins',
                        'desc' => 'Install plugins with one click'
                    ),
                    array(
                        'id' => 'security',
                        'header' => 'Security',
                        'desc' => 'Avoid getting your content stolen'
                    ),
                    array(
                        'id' => 'seo',
                        'header' => 'SEO',
                        'desc' => 'Take action to improve your rankings in Search Engines'
                    ),
                    array(
                        'id' => 'monetize',
                        'header' => 'Monetize',
                        'desc' => 'Monetize your content on autopilot'
                    ),
                    array(
                        'id' => 'others',
                        'header' => 'Others',
                        'desc' => 'Other settings (optimize, footer/header code, email...)'
                    ),
                    array(
                        'id' => 'reset',
                        'header' => 'InstaReset',
                        'desc' => 'Reset all database tables (handle with care!)'
                    ),
                );
            }
         ?>
            <table class="widefat wpfs5-table" style="width:auto">
                <tbody>
                    <?php
                        $i = 1;
                        foreach( $els as $el ) {
                        ?>
                            <tr <?php if ( $i % 2 ) echo 'class="alternate"';?>>
                                <td class="row-title"><label><?php echo $i;?>. <?php echo $el['header'];?></label></td>
                                <td><?php echo $el['desc'];?></td>
                                <td><a href="<?php echo admin_url('admin.php?page=wp-freshstart5-'.$el['id']);?>" class="button button-secondary"> <i class="fa fa-cog"></i> Configure</a></td>
                            </tr>
                        <?php
                            $i++;
                        }
                    ?>
                </tbody>
            </table>
        </p>
        <br/>
	</div>
	<?php
}
}

function wpfresh5_screen_content() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>1. Content</h1>
        <br/>
        <?php wpfresh5_settings_content(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_plugins() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>2. Recommended Plugins</h1>
        <br/>
        <?php wpfresh5_settings_plugins(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_security() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>3. Security</h1>
        <br/>
        <?php wpfresh5_settings_security(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_seo() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>4. SEO</h1>
        <br/>
        <?php wpfresh5_settings_seo(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_branding() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>5. Branding</h1>
        <br/>
        <?php wpfresh5_settings_branding(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_social() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>6. Social</h1>
        <br/>
        <?php wpfresh5_settings_social(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_others() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1><?php if( wpfresh5_is_pro() ) echo '7'; else echo '5';?>. Others</h1>
        <br/>
        <?php wpfresh5_settings_others(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_reset() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>Instareset - <a href="https://wpfreshstart5.com/support/knowledgebase/instareset/" target="_blank" style="font-size:0.8em">Support</a></h1>
        <br/>
        <?php wpfresh5_instareset(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_gdpr() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>3. GDPR  - <a href="https://wpfreshstart5.com/support/knowledgebase/what-is-the-gdpr-and-why-should-i-care/" target="_blank" style="font-size:0.8em">What is GDPR?</a></h1>
        <br/>
        <?php wpfresh5_settings_gdpr(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_legal() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>2. Legal</h1>
        <br/>
        <?php wpfresh5_settings_legal(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_monetize() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>Monetize</h1>
        <br/>
        <?php wpfresh5_settings_monetize(); ?>
        <br/>
    </div>
    <?php
}

function wpfresh5_screen_help() {
    if ( !current_user_can ( 'manage_options' ))
        wp_die ( 'You don\'t have access to this page.' );
    if ( !user_can_access_admin_page ())
        wp_die ( __ ( 'You do not have sufficient permissions to access this page', 'wp-freshstart' ) );
    ?>
    <div class="wrap">
        <img src="<?php echo WPFRESH5_URL;?>/img/wpfs5logo.png" alt="">
        <h1>Help</h1>
        <br/>
        <?php
            if( isset( $_GET['wpf5test']) ) {
                wpfresh5_selftest();
            }
            else
                wpfresh5_help();
}

function wpfresh5_selftest() {
?>
    <h3>Testing your hosting...</h3>
    <?php $curl_enabled = ( extension_loaded('curl') && function_exists('curl_version') ); ?>
    <p><h4 style="padding-left:20px;">Curl enabled <?php wpfresh5_test_check( $curl_enabled ); ?></h4></p>
    <?php if( !$curl_enabled ) { ?>
        <p style="padding-left:30px;">Curl is not enabled in your hosting. This is required to be able to connect to external websites (to import content). Please open a support ticket to your hosting company and ask them to enable curl for your site; after they enable it for you, you will be able to import content using WP Auto Content.</p>
    <?php } ?>
    <?php
        $ext_enabled = wpfresh5_test_ext();
    ?>
    <p><h4 style="padding-left:20px;">External Connections allowed <?php wpfresh5_test_check( $ext_enabled ); ?></h4></p>
    <?php if( !$ext_enabled ) { ?>
        <p style="padding-left:30px;">Your hosting seems to be blocking external connections, so the plugin cannot connect to any sites to get content on autopilot. Please open a support ticket to your hosting company and ask them to enable external connections for your site; after they enable it for you, you will be able to import content using WP Auto Content.</p>
    <?php } ?>
    <?php $php_ok =  ( version_compare( PHP_VERSION, '5.5.0' ) >= 0 ); ?>
    <p><h4 style="padding-left:20px;">PHP Version <?php echo phpversion( ); ?> <?php wpfresh5_test_check( $php_ok ); ?></h4></p>
    <?php if( !$php_ok ) { ?>
        <p style="padding-left:30px;">Your hosting is using a very old PHP version, which is an issue for stability, functionality and speed. Please open a support ticket to your hosting company and ask them to update your PHP version.</p>
    <?php } ?>
    <?php
        global $wp_version;
        $wpv_ok = ( version_compare( $wp_version, '4.4', '>=' ) ); ?>
    <p><h4 style="padding-left:20px;">WordPress Version <?php echo $wp_version; ?> <?php wpfresh5_test_check( $wpv_ok ); ?></h4></p>
    <?php if( !$wpv_ok ) { ?>
        <p style="padding-left:30px;">You are using a very old WordPress version. It is recommended to upgrade to get the latest features and security patches. To upgrade go to <a href="<?php echo admin_url('update-core.php');?>">Dashboard > Updates</a></p>
    <?php } ?>
    <?php
        if( !$curl_enabled || !$ext_enabled || !$php_ok ) {
            echo '<p>Your hosting company does not seem to have all the requirements needed to use WP Auto Content; however it might still work, so we recommend you create a campaign and test. If you can\'t get any content imported, please talk to your hosting company, or move to a different hosting company (we recommend <a href="https://wpfresh5ontent.com/support/bluehost" target="_blank">Bluehost</a> or <a href="https://wpfresh5ontent.com/support/hostgator" target="_blank">Hostgator</a>; WP Auto Content is guaranteed to work there)</p>';
        }
        else {
           echo '<p>Great! Your hosting company seems to be ok.</p><p>However, if you are still having issues with importing, related to your hosting, please talk to them, or move to a different hosting company. We recommend <a href="https://wpfresh5ontent.com/support/bluehost" target="_blank">Bluehost</a> or <a href="https://wpfresh5ontent.com/support/hostgator" target="_blank">Hostgator</a>; WP Freshstart 5 is guaranteed to work there</p>';
        }
    ?>
    <a href="<?php echo admin_url('/admin.php?page=wp-freshstart5-support');?>" class="button button-secondary">Return</a>
<?php
}

function wpfresh5_test_check( $val ) {
    if( $val )
        echo '<i class="fa fa-check" style="color:green"></i>';
    else
        echo '<i class="fa fa-remove" style="color:red"></i>';
}

function wpfresh5_test_ext() {
    $response = wp_remote_get( 'https://knighterrant.s3.amazonaws.com/autotag/test.json' );
    if ( is_array( $response ) ) {
      $header = $response['headers']; // array of http header lines
      $body = $response['body']; // use the content
      if( empty( $body ) )
        return false;
      if( $body == '12345' )
        return true;
    }
    return false;
}

function wpfresh5_help() {
        ?>
        <p>If you are having any issues with the plugin, please do the self-test so you can check if your hosting is 100% compatible with the plugin:</p>
        <p><a href="<?php echo admin_url('/admin.php?page=wp-freshstart5-support&wpf5test=1');?>" class="button button-secondary"><i class="fa fa-check-square-o"></i> Run Self Test</a>
        </p>
        <br/>
        <a class="button button-primary" href="https://wpfreshstart5.com/support" target="_blank">Support</a>
        <br/>
        <!-- <h2>How to use the plugin</h2> -->
        <!--<?php if( wpfresh5_is_pro() ) { ?>
            <iframe width="853" height="480" src="https://www.youtube.com/embed/BxwS2_psph4?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
        <?php } else { ?>
            <iframe width="853" height="480" src="https://www.youtube.com/embed/YCLQw9VuBws?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
        <?php } ?>-->
        <!-- <br/> -->
        <br/>
        <h2>FAQ</h2>
        <h4>1. I have purchased the Pro version, do I need to install both plugins?</h4>
        <p>No, you just need to install the <a href="http://wpfreshstart5.com/v5/pro/" target="_blank">Pro version</a> (which has all the functionality). In fact, if you try to install both, you will get an error, so if you have already installed the basic version, please deactivate and uninstall it first, then install the Pro version</p>
        <h4>2. After configuring some security settings, my site is broken</h4>
        <p>Some of the features in the plugin (especially security) might not be compatible with your hosting company.</p>
        <p>If something is broken, please try the following:</p>
        <p>a) <a target="_blank" href="<?php echo site_url('?wpfresh5_dis_sec=1');?>">click here</a> to disable security settings</p>
        <p>b) If that does not help, review security settings in the WP Freshtart plugin and disable everything</p>
        <p>c) Disable the plugin and see if the problems continue.</p>
        <p>d) <a target="_blank" href="http://customerhelpdesk.in">Open a support ticket</a> letting us know which settings you enabled, the url of your site, and if you are seeing any errors, and our tech team will give you a hand</a>
        <h4>3. I cannot see any images in the gallery</h4>
        <p>If all your images suddenly disappeared, try the following:</p>
        <p>a) Go to WP Freshstart, Security, and disable the checbox "Disable image hotlinking", then click on "Save all changes"
        <p>b) If you still can't see the images, login via ftp and delete the file wp-content/uploads/.htaccess (make a backup first)</p>
        <p>c) <a target="_blank" href="http://customerhelpdesk.in">Open a support ticket</a> and our tech team will give you a hand</a>
        <br/>
        <br/>
        <a class="button button-primary" href="https://wpfreshstart5.com/support" target="_blank">Click here to get support</a>
        <br/>
    </div>
    <?php
}
/* Field Types */

function wpfresh5_field_text( $label = '', $name = '', $value = '', $id = false, $class = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';
?>
    <tr valign="top" <?php echo $row_class;?> <?php if( !$row_visible ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <input type="text" class="regular-text <?php echo $class;?>" name="<?php echo $name;?>" <?php echo $id;?> <?php echo $placeholder;?> value="<?php echo stripslashes( $value ); ?>">
            <?php if( !empty( $help ) ) { ?>
                <p class="description"><?php echo $help;?></p>
            <?php } ?>
        </td>
    </tr>
<?php
}

function wpfresh5_field_password( $label = '', $name = '', $value = '', $id = false, $classes = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';
?>
    <tr valign="top" <?php echo $row_class;?> <?php if( !$row_visible ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <input type="password" class="regular-text" name="<?php echo $name;?>" <?php echo $id;?> <?php echo $placeholder;?> value="<?php echo stripslashes( $value ); ?>">
            <?php if( !empty( $help ) ) { ?>
                <span class="description"><?php echo $help;?></span>
            <?php } ?>
        </td>
    </tr>
<?php
}

function wpfresh5_field_textarea( $label = '', $name = '', $value = '', $id = false, $class = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';
?>
    <tr valign="top" <?php echo $row_class;?> <?php if( !$row_visible ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <textarea class="<?php echo $class;?>" name="<?php echo $name;?>" <?php echo $id;?> <?php echo $placeholder;?>><?php echo stripslashes( $value ); ?></textarea>
            <?php if( !empty( $help ) ) { ?>
                <p class="description"><?php echo $help;?></p>
            <?php } ?>
        </td>
    </tr>
<?php
}

function wpfresh5_field_checkbox( $label = '', $name = '', $value = '', $id = false, $class = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    // var_dump($value);
    if ( $value )
        $checked = checked( $value, 1, false );
    else
        $checked = '';
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';
    if( !empty( $class ) )
        $class = ' class="'.$class.'"';
    ?>
        <tr valign="top" <?php echo $row_class;?> <?php if( !$row_visible ) echo 'style="display:none"';?>>
            <th scope="row"><?php echo $label;?></th>
            <td>
                <input type="checkbox" name="<?php echo $name;?>" <?php echo $id;?> <?php echo $class;?> <?php echo $checked;?> value="1">
                <?php if( !empty( $help ) ) { ?>
                    <p class="description"><?php echo $help;?></p>
                <?php } ?>
            </td>
        </tr>
<?php
}

function wpfresh5_field_checkbox_inner( $label = '', $name = '', $value = '', $id = false, $class = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    if ( $value )
        $checked = checked( true, true, false );
    else
        $checked = '';
        if( !empty( $placeholder ) )
            $placeholder = ' placeholder="'.$placeholder.'"';
        if( !empty( $id ) )
            $id = ' id="'.$id.'"';
        if( empty( $value ) )
            $value = '';
        if( !empty( $row_class ) )
            $row_class = ' class="'.$row_class.'"';
    ?>
        <tr valign="top" <?php echo $row_class;?> <?php if( !$row_visible ) echo 'style="display:none"';?>>
            <th scope="row" style="padding:0;padding-right:10px">&nbsp;</th>
            <td style="padding:0;padding-left:10px">
                <label><input type="checkbox" name="<?php echo $name;?>" <?php echo $id;?> <?php echo $class;?> <?php echo $checked;?> value="1"> <?php echo $label;?></label>
                <?php if( !empty( $help ) ) { ?>
                    <p class="description"><?php echo $help;?></p>
                <?php } ?>
            </td>
        </tr>
<?php
}
function wpfresh5_field_select( $label = '', $name = '', $value = '', $values = array(), $id = false, $help = '', $extra_help = '', $class = '', $row_class = '', $hidden = false, $extra_option = false ) {
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';
?>
    <tr valign="top" <?php echo $row_class;?> <?php if( $hidden ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <select <?php echo $id;?> class="<?php echo $class; ?>" name="<?php echo $name;?>"><?php wpfresh5_select_options( $value, $values, $extra_option );?></select>
            <?php if( !empty( $help ) ) { ?>
                <p class="description"><?php echo $help;?></p>
            <?php } ?>
        </td>
    </tr>
<?php
}

function wpfresh5_field_color( $label = '', $name = '', $value = '', $id = false, $class = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';
?>
    <tr valign="top" <?php echo $row_class;?> <?php if( !$row_visible ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <input type="text" class="regular-text wpfresh5-color-picker <?php echo $class;?>" name="<?php echo $name;?>" <?php echo $id;?> <?php echo $placeholder;?> value="<?php echo stripslashes( $value ); ?>">
            <?php if( !empty( $help ) ) { ?>
                <p class="description"><?php echo $help;?></p>
            <?php } ?>
        </td>
    </tr>
<?php
}

function wpfresh5_field_html( $label = '', $name = '', $value = '', $id = false, $class = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';

    $value = htmlspecialchars_decode( $value );

    $settings = array (
        'textarea_name' => $name,
        'textarea_rows' => 14,
        'editor_height' => 320
        );
    $ta_id = str_replace( array( '[', ']' ), '_', 'wpfresh5_'.$name );
    $value = stripslashes( $value );
?>
    <tr valign="top" <?php echo $row_class;?> <?php if( !$row_visible ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <?php wp_editor( $value, $ta_id, $settings ); ?>
            <?php if( !empty( $help ) ) { ?>
                <p class="description"><?php echo $help;?></p>
            <?php } ?>
        </td>
    </tr>
<?php
}
/* Custom */
function wpfresh5_field_category( $label = '', $name = '', $value = '', $id = false, $classes = '',  $help = '', $placeholder = '' ) {
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
?>
    <tr valign="top">
        <th scope="row"><?php echo $label;?></th>
        <td>
            <?php
                $args = array(
                    'name' => $name,
                    'selected' => $value,
                    'show_option_none' => '- None -',
                    'option_none_value' => 0,
                );
                wp_dropdown_categories( $args );
            ?>
            <?php if( !empty( $help ) ) { ?>
                <span class="description"><?php echo $help;?></span>
            <?php } ?>
        </td>
    </tr>
<?php
}


function wpfresh5_field_author( $label = '', $name = '', $value = '', $id = false, $classes = '',  $help = '', $placeholder = '' ) {
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
?>
    <tr valign="top">
        <th scope="row"><?php echo $label;?></th>
        <td>
            <?php
                wp_dropdown_users(array( 'name' => $name, 'selected' => $value ) );
            ?>
            <?php if( !empty( $help ) ) { ?>
                <br/><span class="description"><?php echo $help;?></span>
            <?php } ?>
        </td>
    </tr>
<?php
}

function wpfresh5_field_custom_post_type( $label = '', $name = '', $values = '', $id = false, $class = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    // if ( $value )
    //     $checked = checked( true, true, false );
    // else
    //     $checked = '';
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';
    if( !empty( $class ) )
        $class = ' class="'.$class.'"';
    ?>
        <tr valign="top" <?php echo $row_class;?> <?php if( !$row_visible ) echo 'style="display:none"';?>>
            <th scope="row"><?php echo $label;?></th>
            <td>
            <?php
                $args = array(
                    'public'   => true,
                  //  '_builtin' => false
                );

                $output = 'objects'; // names or objects, note names is the default
                $operator = 'and'; // 'and' or 'or'

                $post_types = get_post_types( $args, $output, $operator );
                // var_dump($post_types);
                if( $post_types ) {
                    $i = 1;
                    foreach ( $post_types  as $post_type ) {
                        $pt_name = $post_type->name;
                        if( 'attachment' == $pt_name )
                            continue;
                        $pt_label = $post_type->label;
                        $enabled = 0;
                        $checked = '';
                        if( wpfresh5_posttype_checked( $pt_name, $values ) )
                            $checked = 'checked = "checked"';
                ?>
                    <label><input type="checkbox" name="<?php echo $name;?>[<?php echo $pt_name;?>]" <?php echo $class;?> <?php echo $checked;?> value="1"> <?php echo $pt_label;?> </label><br/>
                <?php } } ?>
                <?php if( !empty( $help ) ) { ?>
                    <p class="description"><?php echo $help;?></p>
                <?php } ?>
            </td>
        </tr>
    <?php
}

function wpfresh5_field_image( $label = '', $name = '', $value = '', $id = false, $class = '',  $help = '', $placeholder = '', $row_class = '', $row_visible = true ) {
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    $hide_img = false;

    // if( !empty( $row_class ) )
    //     $row_class = ' class="'.$row_class.'"';
?>
    <tr valign="top" class="file-upload-parent <?php echo $row_class;?>" <?php if( !$row_visible ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <input class="regular-text file-upload-url" type="text" size="36" name="<?php echo $name;?>" value="<?php echo $value;?>" placeholder="Select file..." />
            <button class="button button-secondary wpfresh5_img_upload" type="button"><i class="fa fa-upload"></i> Select Image</button>
            <button class="button button-secondary wpfresh5_img_clear" type="button"><i class="fa fa-remove"></i></button>
            <?php if( !empty( $help ) ) { ?>
                <p class="description"><?php echo $help;?></p>
            <?php } ?>
            <?php if ( !$hide_img) { ?>
                <br/>
                <br/>
                <img src="<?php echo $value; ?>" alt="" class="file-upload-img wpfresh5-admin-img">
            <?php } ?>
        </td>
    </tr>
<?php
}


function wpfresh5_field_image_( $settings, $property, $label = '', $name = '', $id = false, $placeholder = '', $help = '', $extra_help = '', $class = '', $row_class = '', $hidden = false ) {
    if ( isset( $settings->{$property} ) )
        $value = $settings->{$property};
    else
        $value = '';

    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';

    $hide_img = false;
?>
    <tr valign="top" class="file-upload-parent <?php echo $row_class;?>" <?php if( $hidden ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <input class="regular-text file-upload-url" type="text" size="36" name="<?php echo $name;?>" value="<?php echo $value;?>" placeholder="Select file..." />
            <button class="button button-secondary wpfresh5_img_upload" type="button"><span class="fa fa-upload"></span> Select Image</button>
            <?php if( !empty( $help ) ) { ?>
                <span class="description"><?php echo $help;?></span>
            <?php } ?>
            <?php if ( !$hide_img) { ?>
                <br/>
                <br/>
                <img src="<?php echo $value; ?>" alt="" class="file-upload-img wpfresh5-admin-img">
            <?php } ?>
        </td>
    </tr>
<?php
}
/*
function wpfresh5_field_datepicker( $label = '', $name = '', $value = '', $id = false, $classes = '',  $help = '', $placeholder = '' ) {
    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
?>
    <tr valign="top">
        <th scope="row"><?php echo $label;?></th>
        <td>
            <input type="text" class="regular-text" name="<?php echo $name;?>" <?php echo $id;?> <?php echo $placeholder;?> value="<?php echo $value; ?>">
            <?php if( !empty( $help ) ) { ?>
                <span class="description"><?php echo $help;?></span>
            <?php } ?>
        </td>
    </tr>
<?php
}
*/

function wpfresh5_field_hidden( $name = '', $value = '', $id = '', $class='' ) {
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    echo '<input type="hidden" name="'.$name.'" value="'.$value.'" class="'.$class.'" />';
}

/*
function wpfresh5_ifield_html( $settings, $property, $label = '', $name = '', $id = false, $placeholder = '', $help = '', $extra_help = '', $class = '', $row_class = '', $hidden = false ) {
    if ( isset( $settings->{$property} ) )
        $value = htmlspecialchars_decode( $settings->{$property} );
        // $value = $settings->{$property};
    else
        $value = '';

    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';
    if( !empty( $row_class ) )
        $row_class = ' class="'.$row_class.'"';

    $settings = array (
        'textarea_name' => $name,
        'textarea_rows' => 14,
        'editor_height' => 320
        );
    $ta_id = str_replace( array( '[', ']' ), '_', 'wpfresh5_'.$name );
    $value = stripslashes( $value );
?>
    <tr valign="top" <?php echo $row_class;?> <?php if( $hidden ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <?php wp_editor( $value, $ta_id, $settings ); ?>
            <?php if( !empty( $help ) ) { ?>
                <span class="description"><?php echo $help;?></span>
            <?php } ?>
        </td>
    </tr>
<?php
}

function wpfresh5_ifield_image( $settings, $property, $label = '', $name = '', $id = false, $placeholder = '', $help = '', $extra_help = '', $class = '', $row_class = '', $hidden = false ) {
    if ( isset( $settings->{$property} ) )
        $value = $settings->{$property};
    else
        $value = '';

    if( !empty( $placeholder ) )
        $placeholder = ' placeholder="'.$placeholder.'"';
    if( !empty( $id ) )
        $id = ' id="'.$id.'"';
    if( empty( $value ) )
        $value = '';

    $hide_img = false;
?>
    <tr valign="top" class="file-upload-parent <?php echo $row_class;?>" <?php if( $hidden ) echo 'style="display:none"';?>>
        <th scope="row"><?php echo $label;?></th>
        <td>
            <input class="regular-text file-upload-url" type="text" size="36" name="<?php echo $name;?>" value="<?php echo $value;?>" placeholder="Select file..." />
            <button class="button button-secondary wpfresh5_img_upload" type="button"><span class="fa fa-upload"></span> Select Image</button>
            <?php if( !empty( $help ) ) { ?>
                <span class="description"><?php echo $help;?></span>
            <?php } ?>
            <?php if ( !$hide_img) { ?>
                <br/>
                <br/>
                <img src="<?php echo $value; ?>" alt="" class="file-upload-img wpfresh5-banner-img">
            <?php } ?>
        </td>
    </tr>
<?php
}

*/


/* Helper functions */

function wpfresh5_select_options( $current_value = 0, $values=array(), $extra_option = false ) {
    if ($values) {
        if ($extra_option) {
            if( is_string( $extra_option ) )
                $txt = $extra_option;
            else
                $txt = 'Select...';
            ?>
                <option value="0"><?php echo $txt; ?></option>
            <?php
        }
        foreach ($values as $value) {
            // var_dump($value);
            if ($value['value'] === 'optgroup' ) {
                ?>
                    <optgroup label="<?php echo $value['label'];?>">
                <?php
            }   else if (/*$value['value'] &&*/ $value['value'] === 'optgroupclose' ) {
                    ?>
                    </optgroup>
            <?php
                } else {
            ?>
                <option value="<?php echo $value['value'];?>" <?php wpfresh5_selected( $current_value, $value['value'] );?>><?php echo $value['label'];?></option>
            <?php
            }
        }
    }
}

function wpfresh5_checked($current_value, $compare_value=true, $echo=1) {
    if ( $current_value == $compare_value ) {
        if ($echo) {
            echo ' checked="checked" ';
        }
        return ' checked="checked" ';
    }
}

function wpfresh5_selected( $current_value, $compare_value = 'true', $echo = 1 ) {
    if ( $current_value && ( $current_value == $compare_value ) ) {
        if ($echo) {
            echo ' selected="selected" ';
        }
        return ' selected="selected" ';
    }
}

function wpfresh5_get_selected_in_array($haystack, $needle) {
    if ( $haystack ) {
        foreach ( $haystack as $element ) {
            if ( $element['value'] == $needle )
                return $element['label'];
        }
    }
    return '';
}

function wpfresh5_posttype_checked( $pt_name, $values ) {
    if( empty( $values ) )
        return false;
    foreach( $values as $key => $value ) {
        if( $key == $pt_name )
            return true;
    }
    return false;
}


?>