| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Checklist; |
| 4 |
|
| 5 |
use Elementor\Modules\Checklist\Steps\Create_Pages; |
| 6 |
use Elementor\Modules\Checklist\Steps\Step_Base; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Steps_Manager { |
| 13 |
/** @var Step_Base[] $step_instances */ |
| 14 |
private array $step_instances = []; |
| 15 |
|
| 16 |
private Checklist_Module_Interface $module; |
| 17 |
|
| 18 |
public function __construct( Checklist_Module_Interface $module ) { |
| 19 |
$this->module = $module; |
| 20 |
$this->register_steps(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Gets formatted and ordered array of step ( step data, is_marked_completed and is_completed ) |
| 25 |
* |
| 26 |
* @return array |
| 27 |
*/ |
| 28 |
public function get_steps_for_frontend() : array { |
| 29 |
$formatted_steps = []; |
| 30 |
|
| 31 |
foreach ( $this->get_step_ids() as $step_id ) { |
| 32 |
$instance = $this->step_instances[ $step_id ]; |
| 33 |
$is_marked_as_completed = $instance->is_marked_as_completed(); |
| 34 |
|
| 35 |
$step = [ |
| 36 |
'should_allow_undo' => $is_marked_as_completed, |
| 37 |
'is_completed' => $instance->is_immutable_completed() || $instance->is_marked_as_completed() || $instance->is_absolute_completed(), |
| 38 |
'config' => $this->get_step_config( $step_id ), |
| 39 |
]; |
| 40 |
|
| 41 |
$formatted_steps[] = $step; |
| 42 |
} |
| 43 |
|
| 44 |
return $formatted_steps; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Marks a step as completed, returns true if the step was found and marked or false otherwise |
| 49 |
* |
| 50 |
* @param string $step_id |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 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 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Unmarks a step as completed, returns true if the step was found and unmarked or false otherwise |
| 66 |
* |
| 67 |
* @param string $step_id |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 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 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 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 |
| 83 |
* |
| 84 |
* @param $step_id |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 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(); |
| 92 |
|
| 93 |
return; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public function get_step_by_id( string $step_id ) : ?Step_Base { |
| 99 |
return $this->step_instances[ $step_id ] ?? null; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
public function get_step_config( $step_id ) : array { |
| 106 |
$step_instance = $this->step_instances[ $step_id ]; |
| 107 |
|
| 108 |
return $step_instance |
| 109 |
? [ |
| 110 |
'id' => $step_instance->get_id(), |
| 111 |
'title' => $step_instance->get_title(), |
| 112 |
'description' => $step_instance->get_description(), |
| 113 |
'learn_more_text' => $step_instance->get_learn_more_text(), |
| 114 |
'learn_more_url' => $step_instance->get_learn_more_url(), |
| 115 |
'cta_text' => $step_instance->get_cta_text(), |
| 116 |
'cta_url' => $step_instance->get_cta_url(), |
| 117 |
Step_Base::IS_COMPLETION_IMMUTABLE => $step_instance->get_is_completion_immutable(), |
| 118 |
] |
| 119 |
: []; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Getting the step instances array based on source's order |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
private function register_steps() : void { |
| 128 |
foreach ( $this->get_step_ids() as $step_id ) { |
| 129 |
$step_instance = $this->get_step_instance( $step_id ); |
| 130 |
|
| 131 |
if ( $step_instance && ! isset( $this->step_instances[ $step_id ] ) ) { |
| 132 |
$this->step_instances[ $step_id ] = $step_instance; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Using step data->id, instanciates and returns the step class or null if the class does not exist |
| 139 |
* |
| 140 |
* @param $step_data |
| 141 |
* |
| 142 |
* @return Step_Base|null |
| 143 |
*/ |
| 144 |
private function get_step_instance( string $step_id ) : ?Step_Base { |
| 145 |
$class_name = '\\Elementor\\Modules\\Checklist\\Steps\\' . $step_id; |
| 146 |
|
| 147 |
if ( ! class_exists( $class_name ) ) { |
| 148 |
return null; |
| 149 |
} |
| 150 |
|
| 151 |
/** @var Step_Base $step */ |
| 152 |
return new $class_name( $this->module, $this->module->get_wordpress_adapter() ); |
| 153 |
} |
| 154 |
|
| 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 ]; |
| 162 |
} |
| 163 |
} |
| 164 |
|