| @@ -4,9 +4,8 @@ | ||
| 4 | 4 | |
| 5 | 5 | use YayMail\Abstracts\BaseController; |
| 6 | 6 | use YayMail\Models\AddonModel; |
| 7 | 7 | use YayMail\Models\SettingModel; |
| 8 | -use YayMail\Utils\Helpers; | |
| 9 | 8 | use YayMail\Utils\SingletonTrait; |
| 10 | 9 | |
| 11 | 10 | /** |
| 12 | 11 | * Settings Controller |
| @@ -21,12 +20,8 @@ | ||
| 21 | 20 | $this->model = SettingModel::get_instance(); |
| 22 | 21 | $this->init_hooks(); |
| 23 | 22 | } |
| 24 | 23 | |
| 25 | - protected function permission_callback_admin_only() { | |
| 26 | - return current_user_can( 'activate_plugins' ); | |
| 27 | - } | |
| 28 | - | |
| 29 | 24 | protected function init_hooks() { |
| 30 | 25 | register_rest_route( |
| 31 | 26 | YAYMAIL_REST_NAMESPACE, |
| 32 | 27 | '/addons', |
| @@ -44,9 +39,9 @@ | ||
| 44 | 39 | [ |
| 45 | 40 | [ |
| 46 | 41 | 'methods' => \WP_REST_Server::EDITABLE, |
| 47 | 42 | 'callback' => [ $this, 'exec_activate_addon' ], |
| 48 | - 'permission_callback' => [ $this, 'permission_callback_admin_only' ], | |
| 43 | + 'permission_callback' => [ $this, 'permission_callback' ], | |
| 49 | 44 | ], |
| 50 | 45 | ] |
| 51 | 46 | ); |
| 52 | 47 | register_rest_route( |
| @@ -55,9 +50,9 @@ | ||
| 55 | 50 | [ |
| 56 | 51 | [ |
| 57 | 52 | 'methods' => \WP_REST_Server::EDITABLE, |
| 58 | 53 | 'callback' => [ $this, 'exec_deactivate_addon' ], |
| 59 | - 'permission_callback' => [ $this, 'permission_callback_admin_only' ], | |
| 54 | + 'permission_callback' => [ $this, 'permission_callback' ], | |
| 60 | 55 | ], |
| 61 | 56 | ] |
| 62 | 57 | ); |
| 63 | 58 | } |
| @@ -73,83 +68,10 @@ | ||
| 73 | 68 | public function exec_deactivate_addon( \WP_REST_Request $request ) { |
| 74 | 69 | return $this->exec( [ $this, 'deactivate_addon' ], $request ); |
| 75 | 70 | } |
| 76 | 71 | |
| 77 | - public function get_addons( \WP_REST_Request $request ) { | |
| 78 | - $data = AddonModel::get_all(); | |
| 79 | - $platform = $request->get_param( 'platform' ); | |
| 80 | - $data = $this->filter_addons_by_platform( $data, $platform ); | |
| 81 | - $data = $this->reorder_addons_for_catalog( $data ); | |
| 82 | - | |
| 83 | - return array_values( $data ); | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * Keep Addons REST list aligned with active YayMail Woo vs Yay WP Pro (same idea as addons_platform in localize). | |
| 88 | - * | |
| 89 | - * @param array<string, array<string, mixed>> $data Addon rows keyed by namespace. | |
| 90 | - * @return array<string, array<string, mixed>> | |
| 91 | - */ | |
| 92 | - private function filter_addons_by_platform( array $data, string $platform ): array { | |
| 93 | - // Unknown/absent platform falls through to the WordPress catalog, matching | |
| 94 | - // the prior exact-match behavior ( $platform === 'yaymail' ). | |
| 95 | - $platform_obj = '' !== $platform ? \YayMail\Platform\PlatformRegistry::get( $platform ) : null; | |
| 96 | - $is_yaymail = $platform_obj ? $platform_obj->is_woo_product() : false; | |
| 97 | - | |
| 98 | - return array_filter( | |
| 99 | - $data, | |
| 100 | - function ( $addon ) use ( $is_yaymail ) { | |
| 101 | - $categories = isset( $addon['categories'] ) && is_array( $addon['categories'] ) ? $addon['categories'] : []; | |
| 102 | - if ( $is_yaymail ) { | |
| 103 | - return ! in_array( 'wordpress', $categories, true ); //phpcs:ignore | |
| 104 | - } else { | |
| 105 | - return in_array( 'wordpress', $categories, true ); //phpcs:ignore | |
| 106 | - } | |
| 107 | - } | |
| 108 | - ); | |
| 109 | - } | |
| 110 | - | |
| 111 | - /** | |
| 112 | - * Pin core Woo addons first; append all others in original order (WordPress-catalog addons follow). | |
| 113 | - * | |
| 114 | - * @param array<string, array<string, mixed>> $data Addon rows keyed by namespace. | |
| 115 | - * @return array<string, array<string, mixed>> | |
| 116 | - */ | |
| 117 | - private function reorder_addons_for_catalog( array $data ): array { | |
| 118 | - $pinned_keys = [ | |
| 119 | - 'YayMailAddonConditionalLogic', | |
| 120 | - 'YayMailAddonWcSubscription', | |
| 121 | - 'YayMailAddonYITHWishlist', | |
| 122 | - ]; | |
| 123 | - | |
| 124 | - $pinned_set = array_flip( $pinned_keys ); | |
| 125 | - | |
| 126 | - $head = []; | |
| 127 | - $wordpress = []; | |
| 128 | - $others = []; | |
| 129 | - | |
| 130 | - foreach ( $pinned_keys as $key ) { | |
| 131 | - if ( isset( $data[ $key ] ) ) { | |
| 132 | - $head[ $key ] = $data[ $key ]; | |
| 133 | - } | |
| 134 | - } | |
| 135 | - | |
| 136 | - foreach ( $data as $key => $addon ) { | |
| 137 | - if ( isset( $pinned_set[ $key ] ) ) { | |
| 138 | - continue; | |
| 139 | - } | |
| 140 | - | |
| 141 | - $categories = isset( $addon['categories'] ) && is_array( $addon['categories'] ) ? $addon['categories'] : []; | |
| 142 | - $is_wordpress = in_array( 'WordPress', $categories, true ); | |
| 143 | - | |
| 144 | - if ( $is_wordpress ) { | |
| 145 | - $wordpress[ $key ] = $addon; | |
| 146 | - } else { | |
| 147 | - $others[ $key ] = $addon; | |
| 148 | - } | |
| 149 | - } | |
| 150 | - | |
| 151 | - return $head + $wordpress + $others; | |
| 72 | + public function get_addons() { | |
| 73 | + return array_values( AddonModel::get_all() ); | |
| 152 | 74 | } |
| 153 | 75 | |
| 154 | 76 | public function activate_addon( \WP_REST_Request $request ) { |
| 155 | 77 | $addon = $request->get_param( 'addon' ); |