file = str_replace('\\', '/', __FILE__); } /** * Setup the environment for the plugin */ public function bootstrap() { // Install/uninstall actions. register_activation_hook($this->file, array($this, 'activate')); register_deactivation_hook($this->file, array($this, 'deactivate')); register_uninstall_hook($this->file, array('Acumulus', 'uninstall')); // Actions. add_action('admin_menu', array($this, 'addMenuLinks'), 900); add_action('admin_post_acumulus_config', array($this, 'processConfigForm')); add_action('admin_post_acumulus_advanced', array($this, 'processAdvancedForm')); add_action('admin_post_acumulus_batch', array($this, 'processBatchForm')); add_action('woocommerce_order_status_changed', array($this, 'woocommerceOrderStatusChanged'), 10, 3); add_action('woocommerce_order_refunded', array($this, 'woocommerceOrderRefunded'), 10, 2); add_filter('acumulus_invoice_created', array($this, 'acumulusInvoiceCreated'), 10, 3); add_action('add_meta_boxes', array($this, 'addMetaBoxes')); } /** * Helper method for the ConfigStore object to get the version number from the * comment at the top of this file, as is the official location for WordPress * plugins. * * @return string * The version number of this plugin. */ public function getVersionNumber() { if (function_exists('get_plugin_data')) { $plugin_data = get_plugin_data($this->file); $version = $plugin_data['Version']; } else { $version = get_option('acumulus_version'); } return $version; } /** * Helper method to translate strings. * * @param string $key * The key to get a translation for. * * @return string * The translation for the given key or the key itself if no translation * could be found. */ protected function t($key) { return $this->container->getTranslator()->get($key); } /** * Loads our library and creates a configuration object. */ public function init() { if ($this->container === NULL) { // Load autoloader require_once(__DIR__ . '/lib/siel/acumulus/SielAcumulusAutoloader.php'); SielAcumulusAutoloader::register(); // Get language $languageCode = get_bloginfo('language'); if (empty($languageCode)) { $languageCode = 'nl'; } $languageCode = substr($languageCode, 0, 2); // Get WC version to set the shop namespace. /** @var \WooCommerce $woocommerce */ global $woocommerce; $shopNamespace = version_compare($woocommerce->version, '3', '>=') ? 'WooCommerce' : 'WooCommerce\WooCommerce2'; $this->container = new Container($shopNamespace, $languageCode); $this->container->getTranslator()->add(new ModuleTranslations()); } } /** * Adds our pages to the admin menu. */ public function addMenuLinks() { // Start with creating a config form, so we can use the translations. $this->init(); $this->container->getTranslator()->add(new ConfigFormTranslations()); add_submenu_page('options-general.php', $this->t('config_form_title'), $this->t('config_form_header'), 'manage_options', 'acumulus_config', array($this, 'processConfigForm') ); add_submenu_page('options-general.php', $this->t('advanced_form_title'), $this->t('advanced_form_header'), 'manage_options', 'acumulus_advanced', array($this, 'processAdvancedForm') ); // Start with creating the batch form, so we can use the translations. $this->container->getTranslator()->add(new BatchFormTranslations()); add_submenu_page('woocommerce', $this->t('batch_form_title'), $this->t('batch_form_header'), 'manage_woocommerce', 'acumulus_batch', array($this, 'processBatchForm') ); } /** * Implements the admin_post_acumulus_config action. * * Processes and renders the basic config form. */ public function processConfigForm() { $this->processForm('config', 'manage_options'); } /** * Implements the admin_post_acumulus_advanced action. * * Processes and renders the advanced config form. */ public function processAdvancedForm() { $this->processForm('advanced', 'manage_options'); } /** * Implements the admin_post_acumulus_batch action. * * Processes and renders the batch form. */ public function processBatchForm() { $this->processForm('batch', 'manage_woocommerce'); } /** * Processes and renders the form of the given type. * * @param string $type * The form type: config, advanced, or batch. * @param string $capability * The required access right the user should have. */ public function processForm($type, $capability ) { if (!current_user_can($capability)) { wp_die(__('You do not have sufficient permissions to access this page.')); } $form = $this->getForm($type); // Trigger auto update before each form invocation. if (!$this->upgrade()) { $form->addErrorMessages(sprintf($this->t('update_failed'), $this->getVersionNumber())); } $doRemoveAction = false; if ($form->isSubmitted()) { check_admin_referer("acumulus_{$type}_nonce"); if ($type === 'batch') { add_action('woocommerce_valid_order_statuses_for_payment', array($this, 'woocommerceValidOrderStatusesForPayment'), 10, 2); $doRemoveAction = true; } } $form->process(); if ($doRemoveAction) { remove_action('woocommerce_valid_order_statuses_for_payment', array($this, 'woocommerceValidOrderStatusesForPayment'), 10); } $this->renderForm($type, $capability); } /** * Renders the form of the given $type. * * @param string $type * config, advanced, or batch. * @param string $capability * The required access right the user should have. */ protected function renderForm($type, $capability) { if (!current_user_can($capability)) { wp_die(__('You do not have sufficient permissions to access this page.')); } // Add our own CSS. $pluginUrl = plugins_url('/acumulus'); if ($type === 'batch') { wp_enqueue_script('jquery-ui-datepicker'); wp_enqueue_script('acumulus.js', $pluginUrl . '/' . 'acumulus.js'); wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css'); } wp_enqueue_style('acumulus_css_admin', $pluginUrl . '/acumulus.css'); $url = admin_url("admin.php?page=acumulus_{$type}"); // Get our form. $form = $this->getForm($type); // Render the form first, so that any messages added during rendering can be // shown on top. $formOutput = $this->container->getFormRenderer()->render($form); // And kick off rendering the sections. $output = ''; $output .= '
%s