/**
  * Construct the manager
  *
  * @param $extensions
  */
 public function __construct($extensions)
 {
     $apg_licenses = new Licenses($extensions);
     $this->licenses = $apg_licenses->get_licenses();
     $this->extensions = $extensions;
 }
 /**
  * Update the settings to the database options
  *
  * @return array|bool
  */
 public function handle_upload_post()
 {
     $new_settings = filter_input(INPUT_POST, 'apg_settings', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
     if (!is_null($new_settings) && $new_settings !== false) {
         $bool_fields = $this->get_bool_fields();
         foreach ($new_settings as $label => $value) {
             if (in_array($label, $bool_fields)) {
                 $this->options[$label] = true;
                 continue;
             }
             if ($label === 'addons') {
                 $this->options['addons'] = $this->handle_addon_settings($value);
             } elseif (!in_array($label, $bool_fields)) {
                 $this->options[$label] = sanitize_text_field($value);
             }
         }
         foreach ($bool_fields as $b) {
             if (!isset($new_settings[$b])) {
                 $this->options[$b] = false;
             }
         }
         if (isset($new_settings['licenses'])) {
             $apg_licenses = new Licenses($this->extensions);
             $old_licenses = $apg_licenses->get_licenses();
             if (is_array($new_settings['licenses'])) {
                 $activated = 0;
                 $deactivated = 0;
                 foreach ($new_settings['licenses'] as $product => $license) {
                     if (!empty($license)) {
                         if (isset($old_licenses[$product]['key']) && $old_licenses[$product]['key'] === sanitize_text_field($license)) {
                             continue;
                         }
                         if ($apg_licenses->activate_license_key($license, $product)) {
                             $apg_licenses->save_license($license, $product);
                             $activated++;
                         } else {
                             return array('error' => '<strong>' . __('License validation error:', 'apg') . '</strong> ' . esc_attr($apg_licenses->get_error()));
                         }
                     } elseif (isset($old_licenses[$product]['key'])) {
                         if ($apg_licenses->deactivate_license_key($old_licenses[$product]['key'], $product)) {
                             $apg_licenses->unsave_license($product);
                             $deactivated++;
                         } else {
                             return array('error' => '<strong>' . __('License deactivation error:', 'apg') . '</strong> ' . esc_attr($apg_licenses->get_error()));
                         }
                     }
                 }
                 if ($activated >= 1) {
                     $this->apg_success(sprintf(__('We have successfully activated %d license key(s)!', 'apg'), $activated));
                 }
                 if ($deactivated >= 1) {
                     $this->apg_success(sprintf(__('We have successfully deactivated %d license key(s)!', 'apg'), $deactivated));
                 }
             }
         }
         // Don't store the license keys in this option set
         unset($this->options['licenses']);
         return true;
     }
     return array('error' => __('Could not save your settings.', 'apg'));
 }