<?php
/**
 * Theme functions and definitions
 *
 * @package Draly
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

define( 'DRALY_DROPSHIPPING_STARTER_VERSION', '1.0.0.8' );
define( 'DRALY_DROPSHIPPING_STARTER_THEME_DIR', get_template_directory() );
define( 'DRALY_DROPSHIPPING_STARTER_THEME_URI', trailingslashit( get_template_directory_uri() ) );

if ( ! isset( $content_width ) ) {
	$content_width = 900;
}

if ( ! class_exists( 'Draly_Dropshipping_Starter' ) ) {
	class Draly_Dropshipping_Starter {

		protected static $prefix = 'Draly_Dropshipping_Starter_';

		public function __construct() {
			self::initialize();
		}

		public static function initialize() {
			spl_autoload_register( array( __CLASS__, 'draly_dropshipping_starter_autoload' ) );
			add_filter( 'get_default_comment_status', array( __CLASS__, 'draly_dropshipping_starter_open_default_comments_for_page' ), 10, 2 );
			add_filter( 'get_the_archive_title', function ( $title ) {
				return preg_replace( '/^\w+: /', '', $title );
			} );
			add_filter( 'wp_speculation_rules_configuration', '__return_null' );
			add_filter( 'wp_img_tag_add_auto_sizes', function () {
				return false;
			} );
			Draly_Dropshipping_Starter_Pluggable::initialize();
		}

		/**
		 * Method to autoload class declaration file.
		 *
		 * @param string $class_name Name of class to load declaration file for.
		 *
		 * @return  mixed
		 */

		public static function draly_dropshipping_starter_autoload( $class_name ) {
			// Verify class prefix.
			if ( 0 !== strpos( $class_name, self::$prefix ) ) {
				return false;
			}
			// Generate file path from class name.
			$base = DRALY_DROPSHIPPING_STARTER_THEME_DIR . '/includes/';
			$path = strtolower( str_replace( '_', '/', substr( $class_name, strlen( self::$prefix ) ) ) );
			// Check if class file exists.
			$standard    = $path . '.php';
			$alternative = $path . '/' . current( array_slice( explode( '/', str_replace( '\\', '/', $path ) ), - 1 ) ) . '.php';
			while ( true ) {
				// Check if file exists in standard path.
				if ( file_exists( $base . $standard ) ) {
					$exists = $standard;
					break;
				}
				// Check if file exists in alternative path.
				if ( file_exists( $base . $alternative ) ) {
					$exists = $alternative;
					break;
				}
				// If there is no more alternative file, quit the loop.
				if ( false === strrpos( $standard, '/' ) || 0 === strrpos( $standard, '/' ) ) {
					break;
				}
				// Generate more alternative files.
				$standard    = preg_replace( '#/([^/]+)$#', '-\\1', $standard );
				$alternative = implode( '/', array_slice( explode( '/', str_replace( '\\', '/', $standard ) ), 0, - 1 ) ) . '/' . substr( current( array_slice( explode( '/', str_replace( '\\', '/', $standard ) ), - 1 ) ), 0, - 4 ) . '/' . current( array_slice( explode( '/', str_replace( '\\', '/', $standard ) ), - 1 ) );
			}
			// Include class declaration file if exists.
			if ( isset( $exists ) ) {
				include_once $base . $exists;
			}

			return false;
		}

		public static function draly_dropshipping_starter_open_default_comments_for_page( $status, $post_type ) {
			if ( 'page' == $post_type ) {
				return 'open';
			}

			return $status;
		}
	}
}

get_template_part( 'libraries/breadcrumbs' );
get_template_part( 'libraries/class-tgm-plugin-activation' );

new Draly_Dropshipping_Starter();