Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Send_Contact_Email
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 6
110
0.00% covered (danger)
0.00%
0 / 1
 get_slug
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_title
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_description
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_category
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
30
 get_data_type
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Jetpack CRM Automation Send_Contact_Email action.
4 *
5 * @package automattic/jetpack-crm
6 * @since 6.2.0
7 */
8
9namespace Automattic\Jetpack\CRM\Automation\Actions;
10
11use Automattic\Jetpack\CRM\Automation\Data_Types\Contact_Data;
12use Automattic\Jetpack\CRM\Entities\Contact;
13use Automattic\Jetpack\CRM\Entities\Factories\Contact_Factory;
14
15/**
16 * Adds the Send_Contact_Email class.
17 *
18 * @since 6.2.0
19 */
20class Send_Contact_Email extends Base_Send_Email {
21
22    /**
23     * Get the slug name of the step.
24     *
25     * @since 6.2.0
26     *
27     * @return string The slug name of the step.
28     */
29    public static function get_slug(): string {
30        return 'jpcrm/send_contact_email';
31    }
32
33    /**
34     * Get the title of the step.
35     *
36     * @since 6.2.0
37     *
38     * @return string The title of the step.
39     */
40    public static function get_title(): string {
41        return __( 'Send email', 'zero-bs-crm' );
42    }
43
44    /**
45     * Get the description of the step.
46     *
47     * @since 6.2.0
48     *
49     * @return string The description of the step.
50     */
51    public static function get_description(): string {
52        return __( 'Sends an email to a contact', 'zero-bs-crm' );
53    }
54
55    /**
56     * Get the category of the step.
57     *
58     * @since 6.2.0
59     *
60     * @return string The category of the step.
61     */
62    public static function get_category(): string {
63        return __( 'General', 'zero-bs-crm' );
64    }
65
66    /**
67     * Send an email to a contact.
68     *
69     * @since 6.2.0
70     *
71     * @param mixed  $data Data passed from the trigger.
72     * @param ?mixed $previous_data (Optional) The data before being changed.
73     */
74    public function execute( $data, $previous_data = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
75
76        /** @var Contact $contact */
77        $contact = $data->get_data();
78
79        global $zbs;
80
81        if ( empty( $contact->email ) || empty( $this->attributes['body'] ) ) {
82            return;
83        }
84
85        $to_email = $contact->email;
86        $to_name  = $contact->fname;
87        $headers  = array( 'Content-Type: text/html; charset=UTF-8' );
88        $subject  = $this->attributes['subject'] ?? '';
89
90        $is_valid_email = zeroBSCRM_validateEmail( $to_email );
91        if ( ! $is_valid_email ) {
92            return;
93        }
94
95        // get potential contact to use for tracking
96        $potential_contact = $zbs->DAL->contacts->getContact( -1, array( 'email' => $to_email ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
97
98        $contact_dal = Contact_Factory::tidy_data( $contact );
99        $email_body  = jpcrm_mailTemplates_single_send_templated( true, $this->attributes['body'], $subject, $contact_dal );
100
101        $email_data = array(
102            'to_email'     => $to_email,
103            'to_name'      => $to_name,
104            'subject'      => $subject,
105            'headers'      => $headers,
106            'body'         => $email_body,
107            'target_id'    => $potential_contact ? $potential_contact['id'] : -1,
108            'sender_id'    => 1, // legacy
109            'assoc_obj_id' => -999, // legacy
110        );
111
112        $this->send_email( $email_data );
113    }
114
115    /**
116     * {@inheritDoc}
117     */
118    public static function get_data_type(): string {
119        return Contact_Data::class;
120    }
121}