PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / classes / abstract-addon-settings.php

abstract-addon-settings.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/classes/abstract-addon-settings.php

137 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Abstract Addon Settings.
4 *
5 * @version 1.0.0
6 * @since StoreEngine v1.6.7
7 */
8
9 namespace StoreEngine\Classes;
10
11 use StoreEngine\Admin\Settings\Base;
12 use StoreEngine\Interfaces\AddonSettingsInterface;
13 use StoreEngine\Traits\AbstractSingleton;
14 use StoreEngine\Utils\Helper;
15 use StoreEngine\Utils\StringUtil;
16 use WP_Error;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 abstract class AbstractAddonSettings implements AddonSettingsInterface {
23 use AbstractSingleton;
24
25 /**
26 * Addon Settings Name.
27 * Unique name for the addon settings.
28 *
29 * @var string
30 */
31 protected ?string $settings_name = null;
32
33 protected ?array $settings = null;
34
35 protected bool $validate_on_save = false;
36
37 protected function __construct() {
38
39 if ( ! $this->settings_name ) {
40 $this->settings_name = str_replace( ['storeengine-addons-', '-settings'], '', StringUtil::get_class_slug( $this, false ) );
41 }
42
43 $this->dispatch_hooks();
44 }
45
46 public function get_settings_name(): string {
47 return $this->settings_name;
48 }
49
50 /**
51 * Get settings.
52 *
53 * @param string $key
54 * @param mixed $default
55 *
56 * @return mixed
57 */
58 public function get_settings( string $key, $default = null ) {
59 $this->load_settings();
60 // Settings loaded on init so it can use i18n.
61 $value = $this->settings[ $key ] ?? $default;
62
63 return apply_filters( 'storeengine/' . $this->get_settings_name() . '/get_settings', $value, $key );
64 }
65
66 public function load_settings( bool $reload = false ) {
67 if ( null === $this->settings || $reload ) {
68 $this->settings = apply_filters(
69 'storeengine/' . $this->get_settings_name() . '/settings',
70 array_merge(
71 $this->get_default_settings(), // Make sure settings populated correctly without missing any field.
72 (array) Helper::get_settings( $this->get_settings_name(), [] )
73 )
74 );
75 }
76
77 return $this->settings;
78 }
79
80 public function dispatch_hooks() {
81 // These hooks shouldn't be removed.
82
83 add_action( 'init', [ $this, 'load_settings' ] );
84 add_filter( 'storeengine/admin/settings_default_data', fn( $settings ): array => array_merge( $settings, [ $this->get_settings_name() => $this->get_default_settings() ] ) );
85 add_filter( 'storeengine/ajax/settings_fields', fn( array $fields ): array => array_merge( $fields, [ $this->get_settings_name() => $this->get_settings_fields() ] ) );
86
87 if ( $this->validate_on_save ) {
88 add_filter(
89 'storeengine/ajax/validate_settings',
90 function( WP_Error $errors, array $payload ): WP_Error {
91
92 // Only validate this addon's settings when they're part of the
93 // current save. Base-settings saves from other tabs (General,
94 // Compliance, etc.) legitimately omit this key; their stored
95 // values are preserved by Base::prepare_settings_data(), so
96 // there is nothing to validate or reject here.
97 if ( ! array_key_exists( $this->get_settings_name(), $payload ) ) {
98 return $errors;
99 }
100
101 if ( empty( $payload[ $this->get_settings_name() ] ) ) {
102 $errors->add( $this->get_settings_name() . '-settings_data', __( 'Settings data are missing. Try disabling the addon and enable it again.', 'storeengine' ) );
103 return $errors;
104 }
105
106 return $this->validate_settings( $errors, $payload[ $this->get_settings_name() ] );
107 },
108 10,
109 2
110 );
111 }
112 }
113
114 public function save_default_settings(): void {
115 if ( false === Helper::get_settings( $this->get_settings_name(), false ) ) {
116 Base::save_settings( [ $this->get_settings_name() => $this->get_default_settings() ] );
117 }
118 }
119
120 public function update_settings_name( string $old_name ): void {
121 $old_settings = Helper::get_settings( $old_name, false );
122
123 if ( ! $old_settings ) {
124 return;
125 }
126
127 Base::save_settings( [ $this->get_settings_name() => $old_settings ] );
128 Base::delete_settings( $old_name );
129 }
130
131 public function validate_settings( WP_Error $errors, array $payload ): WP_Error {
132 return $errors;
133 }
134 }
135
136 // End of file abstract-addon-settings.php.
137