| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Settings class for licenses |
| 5 |
* |
| 6 |
* @since 1.9 |
| 7 |
*/ |
| 8 |
class PLL_Settings_Licenses extends PLL_Settings_Module { |
| 9 |
protected $items; |
| 10 |
|
| 11 |
/** |
| 12 |
* Constructor |
| 13 |
* |
| 14 |
* @since 1.9 |
| 15 |
* |
| 16 |
* @param object $polylang polylang object |
| 17 |
*/ |
| 18 |
public function __construct( &$polylang ) { |
| 19 |
parent::__construct( |
| 20 |
$polylang, |
| 21 |
array( |
| 22 |
'module' => 'licenses', |
| 23 |
'title' => __( 'License keys', 'polylang' ), |
| 24 |
'description' => __( 'Manage licenses for Polylang Pro and add-ons.', 'polylang' ), |
| 25 |
) |
| 26 |
); |
| 27 |
|
| 28 |
$this->buttons['cancel'] = sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', __( 'Close', 'polylang' ) ); |
| 29 |
|
| 30 |
$this->items = apply_filters( 'pll_settings_licenses', array() ); |
| 31 |
|
| 32 |
add_action( 'wp_ajax_pll_deactivate_license', array( $this, 'deactivate_license' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Tells if the module is active |
| 37 |
* |
| 38 |
* @since 1.9 |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function is_active() { |
| 43 |
return ! empty( $this->items ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Displays the settings form |
| 48 |
* |
| 49 |
* @since 1.9 |
| 50 |
*/ |
| 51 |
protected function form() { |
| 52 |
if ( ! empty( $this->items ) ) { ?> |
| 53 |
<table id="pll-licenses-table" class="form-table"> |
| 54 |
<?php |
| 55 |
foreach ( $this->items as $item ) { |
| 56 |
echo $this->get_row( $item ); // phpcs:ignore WordPress.Security.EscapeOutput |
| 57 |
} |
| 58 |
?> |
| 59 |
</table> |
| 60 |
<?php |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the html for a row (one per license key) for display |
| 66 |
* |
| 67 |
* @since 1.9 |
| 68 |
* |
| 69 |
* @param array $item licence id, name and key |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
protected function get_row( $item ) { |
| 73 |
return $item->get_form_field(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Ajax method to save the license keys and activate the licenses at the same time |
| 78 |
* Overrides parent's method |
| 79 |
* |
| 80 |
* @since 1.9 |
| 81 |
*/ |
| 82 |
public function save_options() { |
| 83 |
check_ajax_referer( 'pll_options', '_pll_nonce' ); |
| 84 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 85 |
wp_die( -1 ); |
| 86 |
} |
| 87 |
|
| 88 |
if ( isset( $_POST['module'] ) && $this->module === $_POST['module'] && ! empty( $_POST['licenses'] ) ) { |
| 89 |
$x = new WP_Ajax_Response(); |
| 90 |
foreach ( $this->items as $item ) { |
| 91 |
if ( ! empty( $_POST['licenses'][ $item->id ] ) ) { |
| 92 |
$updated_item = $item->activate_license( sanitize_key( $_POST['licenses'][ $item->id ] ) ); |
| 93 |
$x->Add( array( 'what' => 'license-update', 'data' => $item->id, 'supplemental' => array( 'html' => $this->get_row( $updated_item ) ) ) ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Updated message |
| 98 |
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' ); |
| 99 |
ob_start(); |
| 100 |
settings_errors(); |
| 101 |
$x->Add( array( 'what' => 'success', 'data' => ob_get_clean() ) ); |
| 102 |
$x->send(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Ajax method to deactivate a license |
| 108 |
* |
| 109 |
* @since 1.9 |
| 110 |
*/ |
| 111 |
public function deactivate_license() { |
| 112 |
check_ajax_referer( 'pll_options', '_pll_nonce' ); |
| 113 |
|
| 114 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 115 |
wp_die( -1 ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! isset( $_POST['id'] ) ) { |
| 119 |
wp_die( 0 ); |
| 120 |
} |
| 121 |
|
| 122 |
$id = substr( sanitize_text_field( wp_unslash( $_POST['id'] ) ), 11 ); |
| 123 |
wp_send_json( |
| 124 |
array( |
| 125 |
'id' => $id, |
| 126 |
'html' => $this->get_row( $this->items[ $id ]->deactivate_license() ), |
| 127 |
) |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|