| @@ -1,14 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Checklist; |
| 4 | 4 | |
| 5 | -use Elementor\Modules\Checklist\Steps\Assign_Homepage; | |
| 6 | 5 | use Elementor\Modules\Checklist\Steps\Create_Pages; |
| 7 | -use Elementor\Modules\Checklist\Steps\Setup_Header; | |
| 8 | -use Elementor\Modules\Checklist\Steps\Add_Logo; | |
| 9 | 6 | use Elementor\Modules\Checklist\Steps\Step_Base; |
| 10 | -use Elementor\Modules\Checklist\Steps\Set_Fonts_And_Colors; | |
| 11 | 7 | |
| 12 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | 9 | exit; // Exit if accessed directly. |
| 14 | 10 | } |
| @@ -16,25 +12,13 @@ | ||
| 16 | 12 | class Steps_Manager { |
| 17 | 13 | /** @var Step_Base[] $step_instances */ |
| 18 | 14 | private array $step_instances = []; |
| 19 | 15 | |
| 20 | - private static array $step_ids = [ | |
| 21 | - Add_Logo::STEP_ID, | |
| 22 | - Set_Fonts_And_Colors::STEP_ID, | |
| 23 | - Create_Pages::STEP_ID, | |
| 24 | - Setup_Header::STEP_ID, | |
| 25 | - Assign_Homepage::STEP_ID, | |
| 26 | - ]; | |
| 27 | - | |
| 28 | 16 | private Checklist_Module_Interface $module; |
| 29 | 17 | |
| 30 | 18 | public function __construct( Checklist_Module_Interface $module ) { |
| 31 | 19 | $this->module = $module; |
| 32 | 20 | $this->register_steps(); |
| 33 | - | |
| 34 | - add_action( 'elementor/init', function() { | |
| 35 | - $this->filter_steps(); | |
| 36 | - } ); | |
| 37 | 21 | } |
| 38 | 22 | |
| 39 | 23 | /** |
| 40 | 24 | * Gets formatted and ordered array of step ( step data, is_marked_completed and is_completed ) |
| @@ -40,19 +24,18 @@ | ||
| 40 | 24 | * Gets formatted and ordered array of step ( step data, is_marked_completed and is_completed ) |
| 41 | 25 | * |
| 42 | 26 | * @return array |
| 43 | 27 | */ |
| 44 | - public function get_steps_for_frontend(): array { | |
| 28 | + public function get_steps_for_frontend() : array { | |
| 45 | 29 | $formatted_steps = []; |
| 46 | 30 | |
| 47 | - foreach ( self::$step_ids as $step_id ) { | |
| 31 | + foreach ( $this->get_step_ids() as $step_id ) { | |
| 48 | 32 | $instance = $this->step_instances[ $step_id ]; |
| 49 | - $instance->maybe_immutably_mark_as_completed(); | |
| 33 | + $is_marked_as_completed = $instance->is_marked_as_completed(); | |
| 50 | 34 | |
| 51 | 35 | $step = [ |
| 52 | - Step_Base::MARKED_AS_COMPLETED_KEY => $instance->is_marked_as_completed(), | |
| 53 | - Step_Base::IMMUTABLE_COMPLETION_KEY => $instance->is_immutable_completed(), | |
| 54 | - Step_Base::ABSOLUTE_COMPLETION_KEY => $instance->is_absolute_completed(), | |
| 36 | + 'should_allow_undo' => $is_marked_as_completed, | |
| 37 | + 'is_completed' => $instance->is_immutable_completed() || $instance->is_marked_as_completed() || $instance->is_absolute_completed(), | |
| 55 | 38 | 'config' => $this->get_step_config( $step_id ), |
| 56 | 39 | ]; |
| 57 | 40 | |
| 58 | 41 | $formatted_steps[] = $step; |
| @@ -60,18 +43,8 @@ | ||
| 60 | 43 | |
| 61 | 44 | return $formatted_steps; |
| 62 | 45 | } |
| 63 | 46 | |
| 64 | - public function update_step( string $step_id, array $data ): void { | |
| 65 | - $step = $this->get_step_by_id( $step_id ); | |
| 66 | - | |
| 67 | - if ( ! $step ) { | |
| 68 | - return; | |
| 69 | - } | |
| 70 | - | |
| 71 | - $step->update_step( $data ); | |
| 72 | - } | |
| 73 | - | |
| 74 | 47 | /** |
| 75 | 48 | * Marks a step as completed, returns true if the step was found and marked or false otherwise |
| 76 | 49 | * |
| 77 | 50 | * @param string $step_id |
| @@ -77,10 +50,16 @@ | ||
| 77 | 50 | * @param string $step_id |
| 78 | 51 | * |
| 79 | 52 | * @return void |
| 80 | 53 | */ |
| 81 | - public function mark_step_as_completed( string $step_id ): void { | |
| 82 | - $this->update_step( $step_id, [ Step_Base::MARKED_AS_COMPLETED_KEY => true ] ); | |
| 54 | + public function mark_step_as_completed( string $step_id ) : void { | |
| 55 | + foreach ( $this->step_instances as $step ) { | |
| 56 | + if ( $step->get_id() === $step_id ) { | |
| 57 | + $step->mark_as_completed(); | |
| 58 | + | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + } | |
| 83 | 62 | } |
| 84 | 63 | |
| 85 | 64 | /** |
| 86 | 65 | * Unmarks a step as completed, returns true if the step was found and unmarked or false otherwise |
| @@ -88,10 +67,16 @@ | ||
| 88 | 67 | * @param string $step_id |
| 89 | 68 | * |
| 90 | 69 | * @return void |
| 91 | 70 | */ |
| 92 | - public function unmark_step_as_completed( string $step_id ): void { | |
| 93 | - $this->update_step( $step_id, [ Step_Base::MARKED_AS_COMPLETED_KEY => false ] ); | |
| 71 | + public function unmark_step_as_completed( string $step_id ) : void { | |
| 72 | + foreach ( $this->step_instances as $step ) { | |
| 73 | + if ( $step->get_id() === $step_id ) { | |
| 74 | + $step->unmark_as_completed(); | |
| 75 | + | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + } | |
| 94 | 79 | } |
| 95 | 80 | |
| 96 | 81 | /** |
| 97 | 82 | * Maybe marks a step as completed (depending on if source allows it), returns true if the step was found and marked or false otherwise |
| @@ -99,19 +84,19 @@ | ||
| 99 | 84 | * @param $step_id |
| 100 | 85 | * |
| 101 | 86 | * @return void |
| 102 | 87 | */ |
| 103 | - public function maybe_set_step_as_immutable_completed( string $step_id ): void { | |
| 104 | - $step = $this->get_step_by_id( $step_id ); | |
| 88 | + public function maybe_set_step_as_immutable_completed( string $step_id ) : void { | |
| 89 | + foreach ( $this->step_instances as $step ) { | |
| 90 | + if ( $step->get_id() === $step_id ) { | |
| 91 | + $step->maybe_mark_as_completed(); | |
| 105 | 92 | |
| 106 | - if ( ! $step ) { | |
| 107 | - return; | |
| 93 | + return; | |
| 94 | + } | |
| 108 | 95 | } |
| 109 | - | |
| 110 | - $step->maybe_immutably_mark_as_completed(); | |
| 111 | 96 | } |
| 112 | 97 | |
| 113 | - public function get_step_by_id( string $step_id ): ?Step_Base { | |
| 98 | + public function get_step_by_id( string $step_id ) : ?Step_Base { | |
| 114 | 99 | return $this->step_instances[ $step_id ] ?? null; |
| 115 | 100 | } |
| 116 | 101 | |
| 117 | 102 | /** |
| @@ -116,9 +101,9 @@ | ||
| 116 | 101 | |
| 117 | 102 | /** |
| 118 | 103 | * @return array |
| 119 | 104 | */ |
| 120 | - public function get_step_config( $step_id ): array { | |
| 105 | + public function get_step_config( $step_id ) : array { | |
| 121 | 106 | $step_instance = $this->step_instances[ $step_id ]; |
| 122 | 107 | |
| 123 | 108 | return $step_instance |
| 124 | 109 | ? [ |
| @@ -126,13 +111,11 @@ | ||
| 126 | 111 | 'title' => $step_instance->get_title(), |
| 127 | 112 | 'description' => $step_instance->get_description(), |
| 128 | 113 | 'learn_more_text' => $step_instance->get_learn_more_text(), |
| 129 | 114 | 'learn_more_url' => $step_instance->get_learn_more_url(), |
| 130 | - Step_Base::IS_COMPLETION_IMMUTABLE => $step_instance->get_is_completion_immutable(), | |
| 131 | 115 | 'cta_text' => $step_instance->get_cta_text(), |
| 132 | 116 | 'cta_url' => $step_instance->get_cta_url(), |
| 133 | - 'image_src' => $step_instance->get_image_src(), | |
| 134 | - 'promotion_data' => $step_instance->get_promotion_data(), | |
| 117 | + Step_Base::IS_COMPLETION_IMMUTABLE => $step_instance->get_is_completion_immutable(), | |
| 135 | 118 | ] |
| 136 | 119 | : []; |
| 137 | 120 | } |
| 138 | 121 | |
| @@ -140,10 +123,10 @@ | ||
| 140 | 123 | * Getting the step instances array based on source's order |
| 141 | 124 | * |
| 142 | 125 | * @return void |
| 143 | 126 | */ |
| 144 | - private function register_steps(): void { | |
| 145 | - foreach ( self::$step_ids as $step_id ) { | |
| 127 | + private function register_steps() : void { | |
| 128 | + foreach ( $this->get_step_ids() as $step_id ) { | |
| 146 | 129 | $step_instance = $this->get_step_instance( $step_id ); |
| 147 | 130 | |
| 148 | 131 | if ( $step_instance && ! isset( $this->step_instances[ $step_id ] ) ) { |
| 149 | 132 | $this->step_instances[ $step_id ] = $step_instance; |
| @@ -151,24 +134,15 @@ | ||
| 151 | 134 | } |
| 152 | 135 | } |
| 153 | 136 | |
| 154 | 137 | /** |
| 155 | - * Returns the steps config from source | |
| 138 | + * Using step data->id, instanciates and returns the step class or null if the class does not exist | |
| 156 | 139 | * |
| 157 | - * @return array | |
| 158 | - */ | |
| 159 | - public static function get_step_ids(): array { | |
| 160 | - return self::$step_ids; | |
| 161 | - } | |
| 162 | - | |
| 163 | - /** | |
| 164 | - * Using step data->id, instantiates and returns the step class or null if the class does not exist | |
| 165 | - * | |
| 166 | 140 | * @param $step_data |
| 167 | 141 | * |
| 168 | 142 | * @return Step_Base|null |
| 169 | 143 | */ |
| 170 | - private function get_step_instance( string $step_id ): ?Step_Base { | |
| 144 | + private function get_step_instance( string $step_id ) : ?Step_Base { | |
| 171 | 145 | $class_name = '\\Elementor\\Modules\\Checklist\\Steps\\' . $step_id; |
| 172 | 146 | |
| 173 | 147 | if ( ! class_exists( $class_name ) ) { |
| 174 | 148 | return null; |
| @@ -174,27 +148,16 @@ | ||
| 174 | 148 | return null; |
| 175 | 149 | } |
| 176 | 150 | |
| 177 | 151 | /** @var Step_Base $step */ |
| 178 | - return new $class_name( $this->module, $this->module->get_wordpress_adapter(), $this->module->get_elementor_adapter() ); | |
| 152 | + return new $class_name( $this->module, $this->module->get_wordpress_adapter() ); | |
| 179 | 153 | } |
| 180 | 154 | |
| 181 | - private function filter_steps() { | |
| 182 | - $step_ids = []; | |
| 183 | - $filtered_steps = apply_filters( 'elementor/checklist/steps', $this->step_instances ); | |
| 184 | - | |
| 185 | - foreach ( $filtered_steps as $step_id => $step_instance ) { | |
| 186 | - if ( ! $step_instance instanceof Step_Base ) { | |
| 187 | - continue; | |
| 188 | - } | |
| 189 | - | |
| 190 | - if ( ! $step_instance->is_visible() ) { | |
| 191 | - continue; | |
| 192 | - } | |
| 193 | - | |
| 194 | - $this->step_instances[ $step_id ] = $step_instance; | |
| 195 | - $step_ids[] = $step_id; | |
| 196 | - } | |
| 197 | - | |
| 198 | - self::$step_ids = $step_ids; | |
| 155 | + /** | |
| 156 | + * Returns the steps config from source | |
| 157 | + * | |
| 158 | + * @return array | |
| 159 | + */ | |
| 160 | + private static function get_step_ids() : array { | |
| 161 | + return [ Create_Pages::STEP_ID ]; | |
| 199 | 162 | } |
| 200 | 163 | } |