php defined( 'ABSPATH' ) or die( 'Can\'t access directly' ); class VR_Theme { /** * A dummy constructor to ensure the class is only initialized once */ function __construct() { /* Do nothing here */ } /** * The real constructor to initialize Limit Login * * @return void */ function initialize() { global $wpdb; $theme = wp_get_theme(); // vars $this->settings = array( // basic 'name' => $theme->get( 'Name' ), 'version' => $theme->get( 'Version' ), // urls 'file' => __FILE__, 'path' => get_stylesheet_directory(), 'url' => get_stylesheet_directory_uri(), ); // constants $this->define( 'VR_THEME_URL', $this->settings['url'] ); $this->define( 'VR_THEME_DIR', $this->settings['path'] ); $this->define( 'VR_THEME_VERSION', $this->settings['version'] ); $this->define( 'VR_MODULES_URL', VR_THEME_URL . '/inc/modules' ); $this->define( 'VR_MODULES_DIR', VR_THEME_DIR . '/inc/modules' ); // load all files $this->loader( [ VR_THEME_DIR . '/vendor/autoload.php', VR_THEME_DIR . '/inc/setup/*.php', VR_THEME_DIR . '/inc/modules/*/autoload.php', ] ); // theme activated add_action( 'after_switch_theme', [ $this, 'activate_theme' ], 10, 2 ); // theme deactivated add_action( 'switch_theme', [ $this, 'deactivate_theme' ], 10, 3 ); // actions add_action( 'init', [ $this, 'init' ], 5 ); } /** * load files * * @param array $files_path * @return void */ private function loader( $files_path ) { foreach ( $files_path as $path ) { foreach ( glob( $path ) as $file ) { require_once $file; } } } /** * This function will run once when theme activated * * @param string $oldname * @param WP_Theme|boolean $oldtheme [description] * @return void */ public function activate_theme( $oldname, $oldtheme = false ) { /** * you can create new database table in here * * @link https://codex.wordpress.org/Creating_Tables_with_Plugins */ flush_rewrite_rules(); // payment reminder if (! wp_next_scheduled ( 'vr_payment_reminder' )) { wp_schedule_event(time(), 'daily', 'vr_payment_reminder'); } } /** * This function will run once when theme deactivated * * @param string $new_name * @param WP_Theme $new_theme * @param WP_Theme $old_theme * @return void */ public function deactivate_theme( $new_name, $new_theme, $old_theme ) { /** * you can delete database table in here * * @link https://codex.wordpress.org/Creating_Tables_with_Plugins */ flush_rewrite_rules(); } /** * This function will run after all plugins and theme functions have been included * * @return void */ function init() { // textdomain $this->load_plugin_textdomain(); } /** * This function will load the textdomain file * * @return void */ function load_plugin_textdomain() { // vars load_theme_textdomain( 'vrtheme', VR_THEME_DIR . '/languages' ); } /** * This function will safely define a constant * * @param string $name * @param mixed $value * @return mixed */ function define( $name, $value = true ) { if ( ! defined( $name ) ) { define( $name, $value ); } } /** * This function will return a value from the settings array found in the kdbm_pbfl object * * @param string $name * @param mixed $value * @return mixed */ function get_setting( $name, $value = null ) { // check settings if ( isset( $this->settings[ $name ] ) ) { $value = $this->settings[ $name ]; } // return return $value; } } /** * The main function responsible for returning the one true vr_theme Instance to functions everywhere. * Use this function like you would a global variable, except without needing to declare the global. * * @since 1.0.0 * @return object main object of vr_theme */ function vr_theme() { global $vr_theme; if ( ! isset( $vr_theme ) ) { $vr_theme = new VR_Theme(); $vr_theme->initialize(); } return $vr_theme; } // initialize vr_theme();