<?php
/**
 * This is the main GeoDirectory plugin file, here we declare and call the important stuff
 *
 * @package     GeoDirectory
 * @copyright   2016 AyeCode Ltd
 * @license     GPL-2.0+
 * @since       1.0.0
 *
 * @wordpress-plugin
 * Plugin Name: GeoDirectory
 * Plugin URI: https://wpgeodirectory.com/
 * Description: GeoDirectory plugin for WordPress.
 * Version: 2.0.0.10-beta
 * Author: AyeCode Ltd
 * Author URI: https://wpgeodirectory.com
 * Text Domain: geodirectory
 * Domain Path: /languages
 * Requires at least: 3.1
 * Tested up to: 4.9.5
 * Update URL: https://github.com/AyeCode/geodirectory/
 */


/**
 * Show WP Easy Updates required admin notification.
 */
if(is_admin()){
	if (!function_exists('ayecode_show_update_plugin_requirement')) {//only load the update file if needed
		function ayecode_show_update_plugin_requirement() {
			if ( !defined( 'WP_EASY_UPDATES_ACTIVE' ) ) {
				?>
				<div class="notice notice-warning is-dismissible">
					<p>
						<strong>
							<?php
							echo sprintf( __( 'The plugin %sWP Easy Updates%s is required to check for and update some installed plugins/themes, please install it now.', 'geodirectory' ), '<a href="https://wpeasyupdates.com/" target="_blank" title="WP Easy Updates">', '</a>' );
							?>
						</strong>
					</p>
				</div>
				<?php
			}
		}
		add_action( 'admin_notices', 'ayecode_show_update_plugin_requirement' );
	}
}

if ( ! class_exists( 'GeoDirectory' ) ) :

/**
 * Main GeoDirectory Class.
 *
 * @class GeoDirectory
 * @version 2.0.0
 */
final class GeoDirectory {
    /**
     * GeoDirectory version.
     *
     * @var string
     */
    public $version = '2.0.0.10-beta';
    
    /**
     * GeoDirectory instance.
     *
     * @access private
     * @since  2.0.0
     * @var    GeoDirectory The one true GeoDirectory
     */
    private static $instance = null;
    
    /**
     * The settings instance variable
     *
     * @access public
     * @since  2.0.0
     * @var    GeoDirectory_Settings
     */
    public $settings;
    
    /**
     * Main GeoDirectory Instance.
     *
     * Ensures only one instance of GeoDirectory is loaded or can be loaded.
     *
     * @since 2.0.0
     * @static
     * @see GeoDir()
     * @return GeoDirectory - Main instance.
     */
    public static function instance() {
        if ( ! isset( self::$instance ) && ! ( self::$instance instanceof GeoDirectory ) ) {
            self::$instance = new GeoDirectory;
            self::$instance->setup_constants();
            
            add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) );

            if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
                add_action( 'admin_notices', array( self::$instance, 'php_version_notice' ) );

                return self::$instance;
            }

            self::$instance->includes();
            self::$instance->init_hooks();

            do_action( 'geodirectory_loaded' );
        }
        
        return self::$instance;
    }
    
    /**
     * Setup plugin constants.
     *
     * @access private
     * @since 2.0.0
     * @return void
     */
    private function setup_constants() {
        global $wpdb, $plugin_prefix, $geodir_post_custom_fields_cache;
        
		$upload_dir = wp_upload_dir( null, false );
        $plugin_prefix = $wpdb->prefix . 'geodir_';
        
        if ( $this->is_request( 'test' ) ) {
            $plugin_path = dirname( __FILE__ );
        } else {
            $plugin_path = plugin_dir_path( __FILE__ );
        }
        
        $this->define( 'GEODIRECTORY_VERSION', $this->version );
        $this->define( 'GEODIRECTORY_PLUGIN_FILE', __FILE__ );
        $this->define( 'GEODIRECTORY_PLUGIN_DIR', $plugin_path );
        $this->define( 'GEODIRECTORY_PLUGIN_URL', untrailingslashit( plugins_url( '/', __FILE__ ) ) );
        $this->define( 'GEODIRECTORY_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
        $this->define( 'GEODIRECTORY_TEXTDOMAIN', 'geodirectory' );
        
        // Database tables
		$this->define( 'GEODIR_API_KEYS_TABLE', $plugin_prefix . 'api_keys' ); // rest api keys table
        $this->define( 'GEODIR_ATTACHMENT_TABLE', $plugin_prefix . 'attachments' ); // attachments table
        $this->define( 'GEODIR_COUNTRIES_TABLE', $plugin_prefix . 'countries' ); // countries table
        $this->define( 'GEODIR_CUSTOM_FIELDS_TABLE', $plugin_prefix . 'custom_fields' ); // custom fields table
        $this->define( 'GEODIR_CUSTOM_SORT_FIELDS_TABLE', $plugin_prefix . 'custom_sort_fields' ); // custom sort fields table
        $this->define( 'GEODIR_ICON_TABLE', $plugin_prefix . 'post_icon' ); // post icon table
        $this->define( 'GEODIR_REVIEW_TABLE', $plugin_prefix . 'post_review' ); // post review table
		$this->define( 'GEODIR_BUSINESS_HOURS_TABLE', $plugin_prefix . 'business_hours' ); // business hours table
		
		$this->define( 'GEODIR_ROUNDING_PRECISION', 4 );
        
        // Do not store any revisions (except the one autosave per post).
        $this->define( 'WP_POST_REVISIONS', 0 );
		
		$this->define( 'GEODIR_REST_SLUG', 'geodir' );
		$this->define( 'GEODIR_REST_API_VERSION', '2' );
        
        // This will store the cached post custom fields per package for each page load so not to run for each listing.
        $geodir_post_custom_fields_cache = array();
    }
    
    /**
     * Loads the plugin language files
     *
     * @access public
     * @since 2.0.0
     * @return void
     */
    public function load_textdomain() {
        global $wp_version;
        
        $locale = $wp_version >= 4.7 ? get_user_locale() : get_locale();
        
        /**
         * Filter the plugin locale.
         *
         * @since   1.4.2
         * @package GeoDirectory
         */
        $locale = apply_filters( 'plugin_locale', $locale, 'geodirectory' );

        load_textdomain( 'geodirectory', WP_LANG_DIR . '/' . 'geodirectory' . '/' . 'geodirectory' . '-' . $locale . '.mo' );
        load_plugin_textdomain( 'geodirectory', FALSE, basename( dirname( GEODIRECTORY_PLUGIN_FILE ) ) . '/languages/' );
    }
    
    /**
     * Show a warning to sites running PHP < 5.3
     *
     * @static
     * @access private
     * @since 2.0.0
     * @return void
     */
    public static function php_version_notice() {
        echo '<div class="error"><p>' . __( 'Your version of PHP is below the minimum version of PHP required by GeoDirectory. Please contact your host and request that your version be upgraded to 5.3 or later.', 'geodirectory' ) . '</p></div>';
    }
    
    /**
     * Include required files.
     *
     * @access private
     * @since 2.0.0
     * @return void
     */
    private function includes() {
        global $pagenow, $geodir_options;

        /**
         * Class autoloader.
         */
        include_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-autoloader.php' );

        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/formatting-functions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/deprecated-functions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/core-functions.php' );
		require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-datetime.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/admin/settings/functions.php' );
        $geodir_options = geodir_get_settings();

        include_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-post-types.php' ); // Registers post types

        //print_r($geodir_options);
        
        if ( !defined( 'GEODIR_LATITUDE_ERROR_MSG' ) ) {
            require_once( GEODIRECTORY_PLUGIN_DIR . 'language.php' ); // Define language constants.
        }



	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-session.php' );
	    GeoDir_Email::init();// set up the email class
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/helper-functions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/validation-functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/user-functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/geodir-ajax-functions.php' ); // @todo remove onece replced with below class-gd-ajax.php
        //require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-gd-ajax.php' );
        GeoDir_AJAX::init();
	    GeoDir_Post_Data::init(); // post data
	    GeoDir_Compatibility::init(); // plugin/theme comaptibility checks

        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/general_functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/custom_functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/listing_filters.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/template-functions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/post_functions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/image-functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/post-types-functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/taxonomy-functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/custom-fields/input-functions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/custom-fields/output-functions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/custom-fields/output-filter-functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/custom-fields/functions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/comments-functions.php' );
	    GeoDir_Comments::init();
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/location_functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/geodir-shortcode-functions.php' );
		require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/business-hours-functions.php' );


        //require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/custom_taxonomy_hooks_actions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/geodirectory_hooks_actions.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/libraries/wp-super-duper.php' );
	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/geodir-widget-functions.php' );

	    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/maps/map_functions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/maps/map_template_tags.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/geodirectory_template_tags.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/geodirectory_template_actions.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-frontend-scripts.php' );
        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-permalinks.php' );

	    if(defined( 'GUTENBERG_DEVELOPMENT_MODE' )){
		    require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/admin/block-types.php' );
	    }
		
		/**
		 * REST API.
		 */
		require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-api.php' );
		require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-auth.php' );
		require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-register-wp-admin-settings.php' );


        if ( $this->is_request( 'admin' ) || $this->is_request( 'test' ) || $this->is_request( 'cli' ) ) {
            if ( !empty( $_REQUEST['taxonomy'] ) ) {
                require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/admin/class-geodir-admin-taxonomies.php' );
            }

	        


	        //include_once( GEODIRECTORY_PLUGIN_DIR . 'includes/admin/class-geodir-admin.php' );
            new GeoDir_Admin(); // init the GD admin class

	        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/admin/admin-functions.php' );
	        require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/admin/diagnostic-functions.php' );
			require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/admin/dashboard-functions.php' );
            GeoDir_Admin_Install::init(); // init the install class
	        GeoDir_Admin_Tracker::init(); // tracking data
            require_once( GEODIRECTORY_PLUGIN_DIR . 'upgrade.php' );

            if( 'plugins.php' === $pagenow ) {
                // Better update message
                $file   = basename( GEODIRECTORY_PLUGIN_FILE );
                $folder = basename( dirname( GEODIRECTORY_PLUGIN_FILE ) );
                $hook   = "in_plugin_update_message-{$folder}/{$file}";
                add_action( $hook, 'geodir_admin_upgrade_notice', 20, 2 );
            }

	        if( 'edit.php' === $pagenow || 'post.php' === $pagenow || 'post-new.php' == $pagenow || ( defined('DOING_AJAX') && DOING_AJAX && $_REQUEST['action']=='inline-save')) {
		        GeoDir_Admin_Post_View::init();
	        }
	        
        }
        
        $this->load_db_language();
        
        if ( $this->is_request( 'frontend' ) ) {
            require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-template-loader.php' ); // Template Loader
            require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/class-geodir-shortcodes.php' ); // Shortcodes class

            // Theme Compatibility
            $compatibility_file = geodir_theme_compatibility_file();
            
            if ( is_file( $compatibility_file ) && file_exists( $compatibility_file ) ) {
                require_once( $compatibility_file );
            }
        }

        $this->query = new GeoDir_Query();
		$this->api   = new GeoDir_API();
    }
    
    /**
     * Hook into actions and filters.
     * @since  2.3
     */
    private function init_hooks() {
        add_action( 'init', array( $this, 'init' ), 0 );
        add_action( 'init', array( 'GeoDir_Shortcodes', 'init' ) );
    }
    
    /**
     * Init GeoDirectory when WordPress Initialises.
     */
    public function init() {
        // Before init action.
        do_action( 'geodirectory_before_init' );

        // Init action.
        do_action( 'geodirectory_init' );
    }
    
    /**
     * Load the language for custom fields, custom text.
     */
    public function load_db_language() {
        $language_file = GEODIRECTORY_PLUGIN_DIR . 'db-language.php';
        
        // Load language string file if not created yet.
        if ( ! file_exists( $language_file ) ) {
            geodirectory_load_db_language();
        }

        if ( file_exists( $language_file ) ) {
            try {
                require_once( $language_file );
            } catch ( Exception $e ) {
                geodir_error_log( $e->getMessage(), 'Language Error' );
            }
        }
    }
    
    /**
     * Define constant if not already set.
     *
     * @param  string $name
     * @param  string|bool $value
     */
    private function define( $name, $value ) {
        if ( ! defined( $name ) ) {
            define( $name, $value );
        }
    }
    
    /**
     * Request type.
     *
     * @param  string $type admin, frontend, ajax, cron, test or CLI.
     * @return bool
     */
    private function is_request( $type ) {
        switch ( $type ) {
            case 'admin' :
                return is_admin();
                break;
            case 'ajax' :
                return defined( 'DOING_AJAX' );
                break;
            case 'cli' :
                return ( defined( 'WP_CLI' ) && WP_CLI );
                break;
            case 'cron' :
                return defined( 'DOING_CRON' );
                break;
            case 'frontend' :
                return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
                break;
            case 'test' :
                return defined( 'GD_TESTING_MODE' );
                break;
        }
        
        return null;
    }
	
	/**
	 * Check the active theme.
	 *
	 * @since  2.0.0
	 * @param  string $theme Theme slug to check.
	 * @return bool
	 */
	private function is_active_theme( $theme ) {
		return get_template() === $theme;
	}
	
	/**
	 * Get the plugin url.
	 *
	 * @return string
	 */
	public function plugin_url() {
		return untrailingslashit( plugins_url( '/', GEODIRECTORY_PLUGIN_FILE ) );
	}

	/**
	 * Get the plugin path.
	 *
	 * @return string
	 */
	public function plugin_path() {
		return untrailingslashit( plugin_dir_path( GEODIRECTORY_PLUGIN_FILE ) );
	}

	/**
	 * Get the template path.
	 *
	 * @return string
	 */
	public function template_path() {
		return trailingslashit( geodir_get_theme_template_dir_name() );
	}

	/**
	 * Get Ajax URL.
	 *
	 * @return string
	 */
	public function ajax_url() {
		return admin_url( 'admin-ajax.php', 'relative' );
	}
}

endif;

/**
 * The main function responsible for returning the one true GeoDirectory
 * Instance to functions everywhere.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 * Example: <?php $geodirectory = geodirectory(); ?>
 *
 * @since 2.0.0
 * @return GeoDirectory The one true GeoDirectory Instance
 */
function GeoDir() {
    return GeoDirectory::instance();
}
// Global for backwards compatibility.
$GLOBALS['geodirectory'] = GeoDir();


//$fields = geodir_default_custom_fields('gd_place');
//
///**
// * Filter the array of default custom fields DB table data.
// *
// * @since 1.0.0
// * @param string $fields The default custom fields as an array.
// */
//$fields = apply_filters('geodir_before_default_custom_fields_saved', $fields);
//foreach ($fields as $field_index => $field) {
//    geodir_custom_field_save($field);
//
//}

//function my_project_updated_send_email( $post_id, $post, $update ) {
//
//    print_r($post);exit;
//}
//add_action( 'wp_insert_post', 'my_project_updated_send_email', 10, 3 );

//function adding_custom_meta_boxes( $post_type, $post ) {
//
//	//print_r($post);exit;
//
//	add_meta_box(
//		'my-meta-box',
//		__( 'My Meta Box' ),
//		'render_my_meta_box',
//		'post',
//		'normal',
//		'default'
//	);
//}
//add_action( 'add_meta_boxes', 'adding_custom_meta_boxes', 1, 2 );
//function render_my_meta_box(){
//	echo 'zzzz';
//}

//@todo we should add a dydanmic text to the theme sidebars that will allow a user to one click add recomended widgets
add_action('dynamic_sidebar_before','xzxzxxz');
function xzxzxxz(){
	//echo '###';
}

