Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Automation_Bootstrap
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 6
306
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 register_data_transformers
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 register_triggers
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
12
 register_conditions
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 register_actions
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 register_workflows
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Bootstrap the Jetpack CRM Automation engine.
4 *
5 * @package automattic/jetpack-crm
6 * @since 6.2.0
7 */
8
9namespace Automattic\Jetpack\CRM\Automation;
10
11/**
12 * Bootstrap the Jetpack CRM Automation engine.
13 *
14 * @since 6.2.0
15 */
16final class Automation_Bootstrap {
17
18    /**
19     * The automation engine we want to bootstrap.
20     *
21     * @since 6.2.0
22     *
23     * @var Automation_Engine
24     */
25    private $engine;
26
27    /**
28     * Initialise the automation engine.
29     *
30     * @since 6.2.0
31     *
32     * @return void
33     */
34    public function init(): void {
35        $this->engine = Automation_Engine::instance();
36
37        $this->register_data_transformers();
38        $this->register_triggers();
39        $this->register_conditions();
40        $this->register_actions();
41        $this->register_workflows();
42    }
43
44    /**
45     * Register data transformers.
46     *
47     * @since 6.2.0
48     *
49     * @return void
50     */
51    protected function register_data_transformers(): void {
52        $data_transformers = array(
53            \Automattic\Jetpack\CRM\Automation\Data_Transformers\Data_Transformer_Invoice_To_Contact::class,
54            \Automattic\Jetpack\CRM\Automation\Data_Transformers\Data_Transformer_Entity_To_Tag_List::class,
55        );
56
57        /**
58         * Filter list of available data transformers for automation steps.
59         *
60         * This can be used to add and/or remove data transformers allowed in automations.
61         *
62         * @since 6.2.0
63         *
64         * @param string[] $var A list of data transformer classes.
65         */
66        $data_transformers = apply_filters( 'jpcrm_automation_data_types', $data_transformers );
67
68        foreach ( $data_transformers as $data_transformer ) {
69            try {
70                $this->engine->register_data_transformer( $data_transformer );
71            } catch ( \Exception $e ) {
72                $this->engine->get_logger()->log( $e->getMessage() );
73            }
74        }
75    }
76
77    /**
78     * Register triggers.
79     *
80     * @since 6.2.0
81     *
82     * @return void
83     */
84    protected function register_triggers(): void {
85        $triggers = array(
86            \Automattic\Jetpack\CRM\Automation\Triggers\Company_Deleted::class,
87            \Automattic\Jetpack\CRM\Automation\Triggers\Company_Created::class,
88            \Automattic\Jetpack\CRM\Automation\Triggers\Company_Status_Updated::class,
89            \Automattic\Jetpack\CRM\Automation\Triggers\Company_Updated::class,
90            \Automattic\Jetpack\CRM\Automation\Triggers\Contact_Before_Deleted::class,
91            \Automattic\Jetpack\CRM\Automation\Triggers\Contact_Deleted::class,
92            \Automattic\Jetpack\CRM\Automation\Triggers\Contact_Email_Updated::class,
93            \Automattic\Jetpack\CRM\Automation\Triggers\Contact_Created::class,
94            \Automattic\Jetpack\CRM\Automation\Triggers\Contact_Status_Updated::class,
95            \Automattic\Jetpack\CRM\Automation\Triggers\Contact_Updated::class,
96            \Automattic\Jetpack\CRM\Automation\Triggers\Task_Created::class,
97            \Automattic\Jetpack\CRM\Automation\Triggers\Task_Deleted::class,
98            \Automattic\Jetpack\CRM\Automation\Triggers\Task_Updated::class,
99            \Automattic\Jetpack\CRM\Automation\Triggers\Invoice_Deleted::class,
100            \Automattic\Jetpack\CRM\Automation\Triggers\Invoice_Created::class,
101            \Automattic\Jetpack\CRM\Automation\Triggers\Invoice_Status_Updated::class,
102            \Automattic\Jetpack\CRM\Automation\Triggers\Invoice_Updated::class,
103            \Automattic\Jetpack\CRM\Automation\Triggers\Quote_Accepted::class,
104            \Automattic\Jetpack\CRM\Automation\Triggers\Quote_Created::class,
105            \Automattic\Jetpack\CRM\Automation\Triggers\Quote_Deleted::class,
106            \Automattic\Jetpack\CRM\Automation\Triggers\Quote_Status_Updated::class,
107            \Automattic\Jetpack\CRM\Automation\Triggers\Quote_Updated::class,
108            \Automattic\Jetpack\CRM\Automation\Triggers\Transaction_Created::class,
109            \Automattic\Jetpack\CRM\Automation\Triggers\Transaction_Updated::class,
110            \Automattic\Jetpack\CRM\Automation\Triggers\WP_User_Created::class,
111        );
112
113        /**
114         * Filter list of available triggers for automations.
115         *
116         * This can be used to add and/or remove triggers allowed in automations.
117         *
118         * @since 6.2.0
119         *
120         * @param string[] $triggers A list of triggers classes.
121         */
122        $triggers = apply_filters( 'jpcrm_automation_triggers', $triggers );
123
124        foreach ( $triggers as $trigger ) {
125            try {
126                $this->engine->register_trigger( $trigger );
127            } catch ( \Exception $e ) {
128                $this->engine->get_logger()->log( $e->getMessage() );
129            }
130        }
131    }
132
133    /**
134     * Register conditions.
135     *
136     * @since 6.2.0
137     *
138     * @return void
139     */
140    protected function register_conditions(): void {
141        $conditions = array(
142            \Automattic\Jetpack\CRM\Automation\Conditions\Contact_Field_Changed::class,
143            \Automattic\Jetpack\CRM\Automation\Conditions\Contact_Transitional_Status::class,
144            \Automattic\Jetpack\CRM\Automation\Conditions\Invoice_Status_Changed::class,
145            \Automattic\Jetpack\CRM\Automation\Conditions\Entity_Tag::class,
146        );
147
148        /**
149         * Filter list of available conditions for automations.
150         *
151         * This can be used to add and/or remove condition allowed in automations.
152         *
153         * @since 6.2.0
154         *
155         * @param string[] $conditions A list of condition classes.
156         */
157        $conditions = apply_filters( 'jpcrm_automation_conditions', $conditions );
158
159        foreach ( $conditions as $condition ) {
160            try {
161                $this->engine->register_step( $condition );
162            } catch ( \Exception $e ) {
163                $this->engine->get_logger()->log( $e->getMessage() );
164            }
165        }
166    }
167
168    /**
169     * Register actions.
170     *
171     * @since 6.2.0
172     *
173     * @return void
174     */
175    protected function register_actions(): void {
176        $actions = array(
177            \Automattic\Jetpack\CRM\Automation\Actions\Add_Contact_Log::class,
178            \Automattic\Jetpack\CRM\Automation\Actions\Add_Remove_Contact_Tag::class,
179            \Automattic\Jetpack\CRM\Automation\Actions\Delete_Contact::class,
180            \Automattic\Jetpack\CRM\Automation\Actions\New_Contact::class,
181            \Automattic\Jetpack\CRM\Automation\Actions\Update_Contact::class,
182            \Automattic\Jetpack\CRM\Automation\Actions\Update_Contact_Status::class,
183            \Automattic\Jetpack\CRM\Automation\Actions\Send_Contact_Email::class,
184        );
185
186        /**
187         * Filter list of available actions for automations.
188         *
189         * This can be used to add and/or remove actions allowed in automations.
190         *
191         * @since 6.2.0
192         *
193         * @param string[] $actions A list of actions class names.
194         */
195        $actions = apply_filters( 'jpcrm_automation_actions', $actions );
196
197        foreach ( $actions as $action ) {
198            try {
199                $this->engine->register_step( $action );
200            } catch ( \Exception $e ) {
201                $this->engine->get_logger()->log( $e->getMessage() );
202            }
203        }
204    }
205
206    /**
207     * Register workflows.
208     *
209     * @since 6.2.0
210     *
211     * @return void
212     */
213    protected function register_workflows(): void {
214        $workflow_repository = new Workflow\Workflow_Repository();
215        $workflows           = $workflow_repository->find_by(
216            array(
217                'active' => true,
218            )
219        );
220
221        /**
222         * Filter list of available workflows.
223         *
224         * This can be used to add and/or remove actions allowed in automations.
225         *
226         * @since 6.2.0
227         *
228         * @param Automation_Workflow[] $workflows A collection of registered workflows.
229         */
230        $workflows = apply_filters( 'jpcrm_automation_workflows', $workflows );
231
232        foreach ( $workflows as $workflow ) {
233            if ( $workflow instanceof Automation_Workflow ) {
234                try {
235                    $this->engine->add_workflow( $workflow, true );
236                } catch ( \Exception $e ) {
237                    $this->engine->get_logger()->log( $e->getMessage() );
238                }
239            }
240        }
241    }
242}