/** * Main plugin class */ class Donation_BD { /** * @var Donation_BD Single instance of this class */ private static $instance; /** * @var Donation_BD_Admin Admin instance */ public $admin; /** * @var Donation_BD_Ajax Ajax handler instance */ public $ajax; /** * @var Donation_BD_Form Form handler instance */ public $form; /** * @var Donation_BD_Checkout Checkout handler instance */ public $checkout; /** * Main Donation_BD Instance */ public static function instance() { if (!isset(self::$instance) && !(self::$instance instanceof Donation_BD)) { self::$instance = new self(); self::$instance->setup(); } return self::$instance; } /** * Setup plugin */ private function setup() { // Load plugin text domain add_action('init', array($this, 'load_textdomain')); // Initialize components $this->init_components(); // Register hooks $this->register_hooks(); } /** * Initialize plugin components */ private function init_components() { // Admin if (is_admin()) { $this->admin = new Donation_BD_Admin(); } // Ajax handler $this->ajax = new Donation_BD_Ajax(); // Form handler $this->form = new Donation_BD_Form(); // Checkout handler $this->checkout = new Donation_BD_Checkout(); // Register donation product type add_action('init', array($this, 'register_donation_product_type')); } /** * Register hooks */ private function register_hooks() { // Add plugin action links add_filter('plugin_action_links_' . plugin_basename(DONATION_BD_FILE), array($this, 'plugin_action_links')); // Add body class add_filter('body_class', array($this, 'add_body_class')); } /** * Register donation product type */ public function register_donation_product_type() { require_once DONATION_BD_PLUGIN_DIR . 'includes/class-wc-product-donation.php'; } /** * Add plugin action links */ public function plugin_action_links($links) { $plugin_links = array( '' . __('Settings', 'donation-bd') . '' ); return array_merge($plugin_links, $links); } /** * Add body class */ public function add_body_class($classes) { if (is_singular('product')) { global $post; $product = wc_get_product($post->ID); if ($product && $product->get_type() === 'donation') { $classes[] = 'donation-product'; } } return $classes; } /** * Get template path with proper type casting */ public static function get_template_path($template_name = '') { $template_name = (string)$template_name; return trailingslashit(DONATION_BD_PLUGIN_DIR . 'templates') . $template_name; } /** * Load a template with proper variable type casting */ public static function get_template($template_name, $args = array()) { if (empty($template_name)) { return ''; } $template_name = (string)$template_name; $template_path = self::get_template_path($template_name); if (!file_exists($template_path)) { return ''; } if (!empty($args) && is_array($args)) { extract($args); } ob_start(); include $template_path; return ob_get_clean(); } /** * Load plugin text domain */ public function load_textdomain() { $domain = 'donation-bd'; $locale = apply_filters('plugin_locale', get_locale(), $domain); // Cast paths to string to prevent null warnings $mofile = (string)sprintf('%1$s-%2$s.mo', $domain, $locale); $mofile_local = (string)trailingslashit(DONATION_BD_PLUGIN_DIR . 'languages') . $mofile; $mofile_global = (string)trailingslashit(WP_LANG_DIR . '/' . $domain) . $mofile; // Try to load from global location first if (file_exists($mofile_global)) { load_textdomain($domain, $mofile_global); } // Then try local location elseif (file_exists($mofile_local)) { load_textdomain($domain, $mofile_local); } // Finally, load from plugin languages directory else { load_plugin_textdomain($domain, false, dirname(plugin_basename(DONATION_BD_FILE)) . '/languages/'); } } }