PluginProbe
Polylang / 3.8.9
Polylang v3.8.9
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / settings / settings-licenses.php

settings-licenses.php in Polylang 3.8.9, at src/settings/settings-licenses.php

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