| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Licenses Settings UI. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Licenses_Settings |
| 6 |
* @version 1.0.0 |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2019, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_Licenses_Settings' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable_Licenses_Settings |
| 19 |
* |
| 20 |
* @final |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
final class Charitable_Licenses_Settings { |
| 24 |
|
| 25 |
/** |
| 26 |
* The single instance of this class. |
| 27 |
* |
| 28 |
* @var Charitable_Licenses_Settings|null |
| 29 |
*/ |
| 30 |
private static $instance = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Create object instance. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
*/ |
| 37 |
private function __construct() { |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns and/or create the single instance of this class. |
| 42 |
* |
| 43 |
* @since 1.2.0 |
| 44 |
* |
| 45 |
* @return Charitable_Licenses_Settings |
| 46 |
*/ |
| 47 |
public static function get_instance() { |
| 48 |
if ( is_null( self::$instance ) ) { |
| 49 |
self::$instance = new self(); |
| 50 |
} |
| 51 |
|
| 52 |
return self::$instance; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Optionally add the licenses tab. |
| 57 |
* |
| 58 |
* @since 1.4.7 |
| 59 |
* |
| 60 |
* @param string[] $tabs Settings tabs. |
| 61 |
* @return string[] |
| 62 |
*/ |
| 63 |
public function maybe_add_licenses_tab( $tabs ) { |
| 64 |
|
| 65 |
$products = charitable_get_helper( 'licenses' )->get_products(); |
| 66 |
|
| 67 |
if ( empty( $products ) ) { |
| 68 |
return $tabs; |
| 69 |
} |
| 70 |
|
| 71 |
$tabs = charitable_add_settings_tab( |
| 72 |
$tabs, |
| 73 |
'licenses', |
| 74 |
__( 'Licenses', 'charitable' ), |
| 75 |
array( |
| 76 |
'index' => 4, |
| 77 |
) |
| 78 |
); |
| 79 |
|
| 80 |
return $tabs; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Add the licenses tab settings fields. |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public function add_licenses_fields() { |
| 92 |
if ( ! charitable_is_settings_view( 'licenses' ) ) { |
| 93 |
return array(); |
| 94 |
} |
| 95 |
|
| 96 |
$fields = array( |
| 97 |
'section' => array( |
| 98 |
'title' => '', |
| 99 |
'type' => 'hidden', |
| 100 |
'priority' => 10000, |
| 101 |
'value' => 'licenses', |
| 102 |
'save' => false, |
| 103 |
), |
| 104 |
'licenses' => array( |
| 105 |
'title' => false, |
| 106 |
'callback' => array( $this, 'render_licenses_table' ), |
| 107 |
'priority' => 4, |
| 108 |
), |
| 109 |
); |
| 110 |
|
| 111 |
foreach ( charitable_get_helper( 'licenses' )->get_products() as $key => $product ) { |
| 112 |
$fields[ $key ] = array( |
| 113 |
'type' => 'text', |
| 114 |
'render' => false, |
| 115 |
'priority' => 6, |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
return $fields; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Add the licenses group. |
| 124 |
* |
| 125 |
* @since 1.0.0 |
| 126 |
* |
| 127 |
* @param string[] $groups Settings groups. |
| 128 |
* @return string[] |
| 129 |
*/ |
| 130 |
public function add_licenses_group( $groups ) { |
| 131 |
$groups['licenses'] = array(); |
| 132 |
return $groups; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Render the licenses table. |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
* |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function render_licenses_table() { |
| 143 |
charitable_admin_view( 'settings/licenses' ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Add an extra button to the Licenses tab to re-check licenses. |
| 148 |
* |
| 149 |
* @since 1.6.0 |
| 150 |
* |
| 151 |
* @param string $button The button HTML. |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
public function add_license_recheck_button( $button ) { |
| 155 |
$licenses = array_filter( charitable_get_helper( 'licenses' )->get_licenses(), 'is_array' ); |
| 156 |
|
| 157 |
if ( empty( $licenses ) ) { |
| 158 |
return $button; |
| 159 |
} |
| 160 |
|
| 161 |
$html = '<input style="margin-left:8px;height:29px;" type="submit" class="button button-secondary" name="recheck" value="' . esc_attr__( 'Save & Re-check All Licenses', 'charitable' ) . '" /></p>'; |
| 162 |
|
| 163 |
return str_replace( |
| 164 |
'</p>', |
| 165 |
$html, |
| 166 |
$button |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Checks for updated license and invalidates status field if not set. |
| 172 |
* |
| 173 |
* @since 1.0.0 |
| 174 |
* |
| 175 |
* @param mixed[] $values The parsed values combining old values & new values. |
| 176 |
* @param mixed[] $new_values The newly submitted values. |
| 177 |
* @return mixed[] |
| 178 |
*/ |
| 179 |
public function save_license( $values, $new_values ) { |
| 180 |
/* If we didn't just submit licenses, stop here. */ |
| 181 |
if ( ! isset( $new_values['licenses'] ) ) { |
| 182 |
return $values; |
| 183 |
} |
| 184 |
|
| 185 |
$re_check = array_key_exists( 'recheck', $_POST ); |
| 186 |
$licenses = $new_values['licenses']; |
| 187 |
|
| 188 |
foreach ( $licenses as $product_key => $license ) { |
| 189 |
$license = trim( $license ); |
| 190 |
|
| 191 |
if ( empty( $license ) ) { |
| 192 |
$values['licenses'][ $product_key ] = ''; |
| 193 |
continue; |
| 194 |
} |
| 195 |
|
| 196 |
$license_data = charitable_get_helper( 'licenses' )->verify_license( $product_key, $license, $re_check ); |
| 197 |
|
| 198 |
if ( empty( $license_data ) ) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
|
| 202 |
$values['licenses'][ $product_key ] = $license_data; |
| 203 |
} |
| 204 |
|
| 205 |
return $values; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
endif; |
| 210 |
|