| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Checklist; |
| 4 |
|
| 5 |
use Elementor\Modules\Checklist\Steps\Assign_Homepage; |
| 6 |
use Elementor\Modules\Checklist\Steps\Create_Pages; |
| 7 |
use Elementor\Modules\Checklist\Steps\Setup_Header; |
| 8 |
use Elementor\Modules\Checklist\Steps\Add_Logo; |
| 9 |
use Elementor\Modules\Checklist\Steps\Step_Base; |
| 10 |
use Elementor\Modules\Checklist\Steps\Set_Fonts_And_Colors; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
class Steps_Manager { |
| 17 |
/** @var Step_Base[] $step_instances */ |
| 18 |
private array $step_instances = []; |
| 19 |
|
| 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 |
private Checklist_Module_Interface $module; |
| 29 |
|
| 30 |
public function __construct( Checklist_Module_Interface $module ) { |
| 31 |
$this->module = $module; |
| 32 |
$this->register_steps(); |
| 33 |
|
| 34 |
add_action( 'elementor/init', function() { |
| 35 |
$this->filter_steps(); |
| 36 |
} ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Gets formatted and ordered array of step ( step data, is_marked_completed and is_completed ) |
| 41 |
* |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
public function get_steps_for_frontend(): array { |
| 45 |
$formatted_steps = []; |
| 46 |
|
| 47 |
foreach ( self::$step_ids as $step_id ) { |
| 48 |
$instance = $this->step_instances[ $step_id ]; |
| 49 |
$instance->maybe_immutably_mark_as_completed(); |
| 50 |
|
| 51 |
$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(), |
| 55 |
'config' => $this->get_step_config( $step_id ), |
| 56 |
]; |
| 57 |
|
| 58 |
$formatted_steps[] = $step; |
| 59 |
} |
| 60 |
|
| 61 |
return $formatted_steps; |
| 62 |
} |
| 63 |
|
| 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 |
/** |
| 75 |
* Marks a step as completed, returns true if the step was found and marked or false otherwise |
| 76 |
* |
| 77 |
* @param string $step_id |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function mark_step_as_completed( string $step_id ): void { |
| 82 |
$this->update_step( $step_id, [ Step_Base::MARKED_AS_COMPLETED_KEY => true ] ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Unmarks a step as completed, returns true if the step was found and unmarked or false otherwise |
| 87 |
* |
| 88 |
* @param string $step_id |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function unmark_step_as_completed( string $step_id ): void { |
| 93 |
$this->update_step( $step_id, [ Step_Base::MARKED_AS_COMPLETED_KEY => false ] ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Maybe marks a step as completed (depending on if source allows it), returns true if the step was found and marked or false otherwise |
| 98 |
* |
| 99 |
* @param $step_id |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function maybe_set_step_as_immutable_completed( string $step_id ): void { |
| 104 |
$step = $this->get_step_by_id( $step_id ); |
| 105 |
|
| 106 |
if ( ! $step ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$step->maybe_immutably_mark_as_completed(); |
| 111 |
} |
| 112 |
|
| 113 |
public function get_step_by_id( string $step_id ): ?Step_Base { |
| 114 |
return $this->step_instances[ $step_id ] ?? null; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @return array |
| 119 |
*/ |
| 120 |
public function get_step_config( $step_id ): array { |
| 121 |
$step_instance = $this->step_instances[ $step_id ]; |
| 122 |
|
| 123 |
return $step_instance |
| 124 |
? [ |
| 125 |
'id' => $step_instance->get_id(), |
| 126 |
'title' => $step_instance->get_title(), |
| 127 |
'description' => $step_instance->get_description(), |
| 128 |
'learn_more_text' => $step_instance->get_learn_more_text(), |
| 129 |
'learn_more_url' => $step_instance->get_learn_more_url(), |
| 130 |
Step_Base::IS_COMPLETION_IMMUTABLE => $step_instance->get_is_completion_immutable(), |
| 131 |
'cta_text' => $step_instance->get_cta_text(), |
| 132 |
'cta_url' => $step_instance->get_cta_url(), |
| 133 |
'image_src' => $step_instance->get_image_src(), |
| 134 |
'promotion_data' => $step_instance->get_promotion_data(), |
| 135 |
] |
| 136 |
: []; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Getting the step instances array based on source's order |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
private function register_steps(): void { |
| 145 |
foreach ( self::$step_ids as $step_id ) { |
| 146 |
$step_instance = $this->get_step_instance( $step_id ); |
| 147 |
|
| 148 |
if ( $step_instance && ! isset( $this->step_instances[ $step_id ] ) ) { |
| 149 |
$this->step_instances[ $step_id ] = $step_instance; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns the steps config from source |
| 156 |
* |
| 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 |
* @param $step_data |
| 167 |
* |
| 168 |
* @return Step_Base|null |
| 169 |
*/ |
| 170 |
private function get_step_instance( string $step_id ): ?Step_Base { |
| 171 |
$class_name = '\\Elementor\\Modules\\Checklist\\Steps\\' . $step_id; |
| 172 |
|
| 173 |
if ( ! class_exists( $class_name ) ) { |
| 174 |
return null; |
| 175 |
} |
| 176 |
|
| 177 |
/** @var Step_Base $step */ |
| 178 |
return new $class_name( $this->module, $this->module->get_wordpress_adapter(), $this->module->get_elementor_adapter() ); |
| 179 |
} |
| 180 |
|
| 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; |
| 199 |
} |
| 200 |
} |
| 201 |
|