/**
  * Important: bind() may be called multiple times
  *
  * Replace wp_mail() after making sure:
  * 1) the plugin has not already bound to wp_mail and
  * 2) wp_mail is available for use
  * 3) the plugin is properly configured.
  * 4) the plugin's prerequisites are met.
  */
 function bind()
 {
     if (!$this->bound) {
         $binderOptions = PostmanOptions::getInstance();
         $binderAuthorizationToken = PostmanOAuthToken::getInstance();
         $ready = true;
         if (function_exists('wp_mail')) {
             // If the function exists, it's probably because another plugin has
             // replaced the pluggable function first, and we set an error flag.
             // this is an error message because it is a Bind error
             $this->logger->error('wp_mail is already bound, Postman can not use it');
             $this->bindError = true;
         }
         if (!PostmanTransportRegistry::getInstance()->isPostmanReadyToSendEmail($binderOptions, $binderAuthorizationToken)) {
             // this is a debug message because bound may be called again with a NEW transport that IS configured
             // this is a debug message because it is not up to the Binder to report transport errors
             $this->logger->debug('Transport is not configured and ready');
             $ready = false;
         }
         if (!PostmanPreRequisitesCheck::isReady()) {
             // this is a debug message because it is not up to the Binder to report transport errors
             $this->logger->debug('Prerequisite check failed');
             $ready = false;
         }
         if ($ready && !$this->bindError) {
             $this->logger->debug('Binding to wp_mail()');
             $this->replacePluggableFunctionWpMail();
         }
     }
 }
 /**
  * Important: bind() may be called multiple times
  *
  * Replace wp_mail() after making sure:
  * 1) the plugin has not already bound to wp_mail and
  * 2) wp_mail is available for use
  * 3) the plugin is properly configured.
  * 4) the plugin's prerequisites are met.
  */
 function bind()
 {
     if (!$this->bound) {
         $ready = true;
         if (function_exists('wp_mail')) {
             // If the function exists, it's probably because another plugin has
             // replaced the pluggable function first, and we set an error flag.
             // this is an error message because it is a Bind error
             if ($this->logger->isTrace()) {
                 $this->logger->trace('wp_mail is already bound, Postman can not use it');
             }
             $this->bindError = true;
             $ready = false;
         }
         if (!PostmanPreRequisitesCheck::isReady()) {
             // this is a debug message because it is not up to the Binder to report transport errors
             if ($this->logger->isTrace()) {
                 $this->logger->trace('Prerequisite check failed');
             }
             $ready = false;
         }
         if ($ready) {
             if ($this->logger->isTrace()) {
                 $this->logger->trace('Binding to wp_mail()');
             }
             $this->replacePluggableFunctionWpMail();
         }
     }
 }
 /**
  * Print the human-readable plugin state
  */
 public function print_postman_status()
 {
     if (!PostmanPreRequisitesCheck::isReady()) {
         printf('<p><span style="color:red">%s</span></p>', __('Error: Postman is missing a required PHP library.', Postman::TEXT_DOMAIN));
     } else {
         if ($this->wpMailBinder->isUnboundDueToException()) {
             printf('<p><span style="color:red">%s</span></p>', __('Postman: wp_mail has been declared by another plugin or theme, so you won\'t be able to use Postman until the conflict is resolved.', Postman::TEXT_DOMAIN));
         } else {
             if ($this->options->getRunMode() != PostmanOptions::RUN_MODE_PRODUCTION) {
                 printf('<p><span style="background-color:yellow">%s</span></p>', __('Postman is in <em>non-Production</em> mode and is dumping all emails.', Postman::TEXT_DOMAIN));
             } else {
                 if (PostmanTransportRegistry::getInstance()->getSelectedTransport()->isConfiguredAndReady()) {
                     printf('<p class="wp-menu-image dashicons-before dashicons-email"> %s </p>', sprintf(_n('<span style="color:green">Postman is configured</span> and has delivered <span style="color:green">%d</span> email.', '<span style="color:green">Postman is configured</span> and has delivered <span style="color:green">%d</span> emails.', PostmanState::getInstance()->getSuccessfulDeliveries(), Postman::TEXT_DOMAIN), PostmanState::getInstance()->getSuccessfulDeliveries()));
                 } else {
                     printf('<p><span style="color:red">%s</span></p>', __('Postman is <em>not</em> configured and is mimicking out-of-the-box WordPress email delivery.', Postman::TEXT_DOMAIN));
                 }
             }
             $currentTransport = PostmanTransportRegistry::getInstance()->getActiveTransport();
             $deliveryDetails = $currentTransport->getDeliveryDetails($this->options);
             printf('<p>%s</p>', $deliveryDetails);
         }
     }
 }
 /**
  * Print the human-readable plugin state
  */
 public function print_postman_status()
 {
     if (!PostmanPreRequisitesCheck::isReady()) {
         printf('<p><span style="color:red">%s</span></p>', __('Error: Postman is missing a required PHP library.', 'postman-smtp'));
     } else {
         if ($this->wpMailBinder->isUnboundDueToException()) {
             printf('<p><span style="color:red">%s</span></p>', __('Postman: wp_mail has been declared by another plugin or theme, so you won\'t be able to use Postman until the conflict is resolved.', 'postman-smtp'));
         } else {
             if ($this->options->getRunMode() != PostmanOptions::RUN_MODE_PRODUCTION) {
                 printf('<p><span style="background-color:yellow">%s</span></p>', __('Postman is in <em>non-Production</em> mode and is dumping all emails.', 'postman-smtp'));
             } else {
                 if (PostmanTransportRegistry::getInstance()->isPostmanReadyToSendEmail($this->options, $this->authorizationToken)) {
                     printf('<p class="wp-menu-image dashicons-before dashicons-email"> %s </p>', sprintf(_n('<span style="color:green">Postman is configured</span> and has delivered <span style="color:green">%d</span> email.', '<span style="color:green">Postman is configured</span> and has delivered <span style="color:green">%d</span> emails.', PostmanStats::getInstance()->getSuccessfulDeliveries(), 'postman-smtp'), PostmanStats::getInstance()->getSuccessfulDeliveries()));
                     $currentTransport = PostmanTransportRegistry::getInstance()->getCurrentTransport();
                     $deliveryDetails = $currentTransport->getDeliveryDetails($this->options);
                     printf('<p>%s</p>', $deliveryDetails);
                 } else {
                     printf('<p><span>%s %s</span></p>', __('Postman is <em>not</em> handling email delivery.', 'postman-smtp'), sprintf(__('<a href="%s">Configure</a> the plugin.', 'postman-smtp'), PostmanUtils::getSettingsPageUrl()));
                 }
             }
         }
     }
 }
 private function getPhpDependencies()
 {
     $apl = PostmanPreRequisitesCheck::getState();
     $pluginText = array();
     foreach ($apl as $p) {
         array_push($pluginText, $p['name'] . '=' . ($p['ready'] ? 'Yes' : 'No'));
     }
     return implode(', ', $pluginText);
 }
 private function checkPreRequisites()
 {
     $states = PostmanPreRequisitesCheck::getState();
     foreach ($states as $state) {
         if (!$state['ready']) {
             /* Translators: where %1$s is the name of the library */
             $message = sprintf(__('This PHP installation requires the <b>%1$s</b> library.', 'postman-smtp'), $state['name']);
             if ($state['required']) {
                 $this->messageHandler->addError($message);
             } else {
                 // $this->messageHandler->addWarning ( $message );
             }
         }
     }
 }
 /**
  * Options page callback
  */
 public function outputDefaultContent()
 {
     // Set class property
     print '<div class="wrap">';
     $this->displayTopNavigation();
     if (PostmanTransportRegistry::getInstance()->isPostmanReadyToSendEmail($this->options, $this->authorizationToken) && PostmanPreRequisitesCheck::isReady()) {
         if ($this->options->getRunMode() != PostmanOptions::RUN_MODE_PRODUCTION) {
             printf('<p><span style="background-color:yellow">%s</span></p>', __('Postman is in <em>non-Production</em> mode and is dumping all emails.', 'postman-smtp'));
         } else {
             printf('<p><span style="color:green;padding:2px 5px; font-size:1.2em">%s</span></p>', __('Postman is configured.', 'postman-smtp'));
             $currentTransport = PostmanTransportRegistry::getInstance()->getCurrentTransport();
             $deliveryDetails = $currentTransport->getDeliveryDetails($this->options);
             printf('<p style="margin:0 10px"><span>%s</span></p>', $deliveryDetails);
         }
         /* translators: where %d is the number of emails delivered */
         printf('<p style="margin:10px 10px"><span>%s', sprintf(_n('Postman has delivered <span style="color:green">%d</span> email for you.', 'Postman has delivered <span style="color:green">%d</span> emails for you.', PostmanStats::getInstance()->getSuccessfulDeliveries(), 'postman-smtp'), PostmanStats::getInstance()->getSuccessfulDeliveries()));
         if ($this->options->isMailLoggingEnabled()) {
             print ' ';
             printf('<a href="%s">%s</a>.</span></p>', PostmanUtils::getEmailLogPageUrl(), __('View the log', 'postman-smtp'));
         }
         if (PostmanState::getInstance()->isTimeToReviewPostman() && !PostmanOptions::getInstance()->isNew()) {
             print '</br><hr width="70%"></br>';
             /* translators: where %s is the URL to the WordPress.org review and ratings page */
             printf('%s</span></p>', sprintf(__('Please consider <a href="%s">leaving a review</a> to help spread the word! :D', 'postman-smtp'), 'https://wordpress.org/support/view/plugin-reviews/postman-smtp?filter=5'));
         }
     } else {
         printf('<p><span style="color:red; padding:2px 5px; font-size:1.1em">%s</span></p>', __('Postman is <em>not</em> handling email delivery.', 'postman-smtp'));
         if ($this->options->isNew()) {
             printf('<h3>%s</h3>', __('Thank-you for choosing Postman!', 'postman-smtp'));
             /* translators: where %s is the URL of the Setup Wizard */
             printf('<p><span>%s</span></p>', sprintf(__('Let\'s get started! All users are strongly encouraged to <a href="%s">run the Setup Wizard</a>.', 'postman-smtp'), $this->getPageUrl(self::CONFIGURATION_WIZARD_SLUG)));
         }
     }
 }
 /**
  */
 public function outputPurgeDataContent()
 {
     $importTitle = __('Import', Postman::TEXT_DOMAIN);
     $exportTile = __('Export', Postman::TEXT_DOMAIN);
     $resetTitle = __('Reset Plugin', Postman::TEXT_DOMAIN);
     $options = $this->options;
     print '<div class="wrap">';
     PostmanViewController::outputChildPageHeader(sprintf('%s/%s/%s', $importTitle, $exportTile, $resetTitle));
     print '<section id="export_settings">';
     printf('<h3><span>%s<span></h3>', $exportTile);
     printf('<p><span>%s</span></p>', __('Copy this data into another instance of Postman to duplicate the configuration.', Postman::TEXT_DOMAIN));
     $data = '';
     if (!PostmanPreRequisitesCheck::checkZlibEncode()) {
         $extraDeleteButtonAttributes = sprintf('disabled="true"');
         $data = '';
     } else {
         $extraDeleteButtonAttributes = '';
         if (!$options->isNew()) {
             $data = $options->export();
         }
     }
     printf('<textarea cols="80" rows="5" readonly="true" name="settings" %s>%s</textarea>', $extraDeleteButtonAttributes, $data);
     print '</section>';
     print '<section id="import_settings">';
     printf('<h3><span>%s<span></h3>', $importTitle);
     print '<form method="POST" action="' . get_admin_url() . 'admin-post.php">';
     wp_nonce_field(PostmanAdminController::IMPORT_SETTINGS_SLUG);
     printf('<input type="hidden" name="action" value="%s" />', PostmanAdminController::IMPORT_SETTINGS_SLUG);
     print '<p>';
     printf('<span>%s</span>', __('Paste data from another instance of Postman here to duplicate the configuration.', Postman::TEXT_DOMAIN));
     if (PostmanTransportRegistry::getInstance()->getSelectedTransport()->isOAuthUsed(PostmanOptions::getInstance()->getAuthenticationType())) {
         $warning = __('Warning', Postman::TEXT_DOMAIN);
         $errorMessage = __('Using the same OAuth 2.0 Client ID and Client Secret from this site at the same time as another site will cause failures.', Postman::TEXT_DOMAIN);
         printf(' <span><b>%s</b>: %s</span>', $warning, $errorMessage);
     }
     print '</p>';
     printf('<textarea cols="80" rows="5" name="settings" %s></textarea>', $extraDeleteButtonAttributes);
     submit_button(__('Import', Postman::TEXT_DOMAIN), 'primary', 'import', true, $extraDeleteButtonAttributes);
     print '</form>';
     print '</section>';
     print '<section id="delete_settings">';
     printf('<h3><span>%s<span></h3>', $resetTitle);
     print '<form method="POST" action="' . get_admin_url() . 'admin-post.php">';
     wp_nonce_field(PostmanAdminController::PURGE_DATA_SLUG);
     printf('<input type="hidden" name="action" value="%s" />', PostmanAdminController::PURGE_DATA_SLUG);
     printf('<p><span>%s</span></p><p><span>%s</span></p>', __('This will purge all of Postman\'s settings, including account credentials and the email log.', Postman::TEXT_DOMAIN), __('Are you sure?', Postman::TEXT_DOMAIN));
     $extraDeleteButtonAttributes = 'style="background-color:red;color:white"';
     if ($this->options->isNew()) {
         $extraDeleteButtonAttributes .= ' disabled="true"';
     }
     submit_button($resetTitle, 'delete', 'submit', true, $extraDeleteButtonAttributes);
     print '</form>';
     print '</section>';
     print '</div>';
 }
 /**
  *
  * @param unknown $data        	
  */
 public function import($data)
 {
     if (PostmanPreRequisitesCheck::checkZlibEncode()) {
         $logger = new PostmanLogger(get_class($this));
         $logger->debug('Importing Settings');
         $base64 = $data;
         $logger->trace($base64);
         $gz = base64_decode($base64);
         $logger->trace($gz);
         $json = @gzuncompress($gz);
         $logger->trace($json);
         if (!empty($json)) {
             $data = json_decode($json, true);
             $logger->trace($data);
             // overwrite the current version with the version from the imported options
             // this way database upgrading can occur
             $postmanState = get_option('postman_state');
             $postmanState['version'] = $data['version'];
             $logger->trace(sprintf('Setting Postman version to %s', $postmanState['version']));
             assert($postmanState['version'] == $data['version']);
             update_option('postman_state', $postmanState);
             $this->options = $data;
             $logger->info('Imported data');
             $this->save();
             return true;
         } else {
             $logger->error('Could not import data - data error');
             return false;
         }
     }
 }