LLMS_Emails

LLMS_Emails


Source Source

File: includes/class.llms.emails.php

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
class LLMS_Emails {
 
    use LLMS_Trait_Singleton;
 
    /**
     * Class names of all emails
     *
     * @var string[]
     */
    public $emails;
 
    /**
     * Constructor
     *
     * Initializes class.
     * Adds actions to trigger emails off of events.
     *
     * @since 1.0.0
     * @since 3.8.0 Unknown.
     * @since 6.0.0 Removed loading of class files that don't instantiate their class in favor of autoloading.
     *
     * @return void
     */
    private function __construct() {
 
        // Template functions.
        llms()->include_template_functions();
 
        // Email base class.
        $this->emails['generic'] = 'LLMS_Email';
 
        // Email child classes.
        $this->emails['engagement']     = 'LLMS_Email_Engagement';
        $this->emails['reset_password'] = 'LLMS_Email_Reset_Password';
 
        $this->emails = apply_filters( 'lifterlms_email_classes', $this->emails );
    }
 
    /**
     * Get a string of inline CSS to add to an email button
     *
     * Use {button_style} merge code to output in HTML emails.
     *
     * @since 3.8.0
     *
     * @return string
     */
    public function get_button_style() {
        /**
         * Filters the default email button CSS rules
         *
         * @since 3.8.0
         *
         * @param array $email_button_css Associative array of the type css-property => definition.
         */
        $rules  = apply_filters(
            'llms_email_button_css',
            array(
                'background-color' => $this->get_css( 'button-background-color', false ),
                'color'            => $this->get_css( 'button-font-color', false ),
                'display'          => 'inline-block',
                'padding'          => '10px 15px',
                'text-decoration'  => 'none',
            )
        );
        $styles = '';
        foreach ( $rules as $rule => $style ) {
            $styles .= sprintf( '%1$s:%2$s !important;', $rule, $style );
        }
        return $styles;
    }
 
    /**
     * Get css rules specific to the the email templates
     *
     * @since 3.8.0
     * @since 5.2.0 Early bail if no rule is provided.
     *
     * @param string  $rule Optional. Name of the css rule. Default is empty string.
     *                      If not provided an empty string will be returned/echoed.
     * @param boolean $echo Optional. If true, echo the definition. Default is `true`.
     * @return string
     */
    public function get_css( $rule = '', $echo = true ) {
 
        if ( empty( $rule ) ) {
            return '';
        }
 
        /**
         * Filters the default email CSS rules
         *
         * @since 3.8.0
         *
         * @param array $email_css Associative array of the type css-property => definition.
         */
        $css = apply_filters(
            'llms_email_css',
            array(
                'background-color'         => '#f6f6f6',
                'border-radius'            => '3px',
                'button-background-color'  => '#2295ff',
                'button-font-color'        => '#ffffff',
                'divider-color'            => '#cecece',
                'font-color'               => '#222222',
                'font-family'              => 'sans-serif',
                'font-size'                => '15px',
                'font-size-small'          => '13px',
                'heading-background-color' => '#2295ff',
                'heading-font-color'       => '#ffffff',
                'main-color'               => '#2295ff',
                'max-width'                => '580px',
            )
        );
 
        if ( isset( $css[ $rule ] ) ) {
 
            if ( $echo ) {
                echo $css[ $rule ];
            }
 
            return $css[ $rule ];
 
        }
 
    }
 
    /**
     * Get an HTML divider for use in HTML emails
     *
     * Can use shortcode {divider} to output in any email.
     *
     * @since 3.8.0
     *
     * @return string
     */
    public function get_divider_html() {
        return '<div style="height:1px;width:100%;margin:15px auto;background-color:' . $this->get_css( 'divider-color', false ) . '"></div>';
    }
 
    /**
     * Retrieve a new instance of an email
     *
     * @since 3.8.0
     *
     * @param string $id   Email id.
     * @param array  $args Optional arguments to pass to the email.
     * @return LLMS_Email
     */
    public function get_email( $id, $args = array() ) {
 
        $emails = $this->get_emails();
 
        // If we have an email matching the ID, return an instance of that email class.
        if ( isset( $emails[ $id ] ) ) {
            return new $emails[ $id ]( $args );
        }
 
        // Otherwise return a generic email and set the ID to be the requested ID.
        /** @var LLMS_Email $generic */
        $generic = new $emails['generic']( $args );
        $generic->set_id( $id );
        return $generic;
 
    }
 
    /**
     * Get all email objects
     *
     * @since 1.0.0
     *
     * @return string[] Array of all email class names.
     */
    public function get_emails() {
        return $this->emails;
    }
 
    /**
     * Retrieve the source url of the header image as defined in LifterLMS settings
     *
     * @since 3.8.0
     *
     * @return string
     */
    public function get_header_image_src() {
        $src = get_option( 'lifterlms_email_header_image', '' );
        if ( is_numeric( $src ) ) {
            $attachment = wp_get_attachment_image_src( $src, 'full' );
            $src        = $attachment ? $attachment[0] : '';
        }
        /**
         * Filters the header image src
         *
         * @since 3.8.0
         *
         * @param string $src Image `src` attribute value.
         */
        return apply_filters( 'llms_email_header_image_src', $src );
    }
 
    /**
     * Returns an array with the table's tags inline style
     *
     * It makes sure that all the required tags (table, tr, td) are set.
     *
     * @since 5.2.0
     *
     * @return array {
     *     Array of table style.
     *
     *     @type string $0 Style of the table tag.
     *     @type string $1 Style of the tr tag.
     *     @type string $2 Style of the td tag.
     * }
     */
    private function get_parsed_table_style() {
 
        $table_style = $this->get_table_style();
        $table_style = is_array( $table_style ) ? $table_style : array( $table_style );
 
        $table_style = wp_parse_args(
            $table_style,
            array(
                'table' => '',
                'tr'    => '',
                'td'    => '',
            )
        );
 
        return array_values( $table_style );
 
    }
 
    /**
     * Return an associative array with the table's tags inline style
     *
     * @since 5.2.0
     *
     * @return string
     */
    protected function get_table_style() {
        return array(
            'table' => $this->get_table_table_style(),
            'tr'    => $this->get_table_tr_style(),
            'td'    => $this->get_table_td_style(),
        );
    }
 
    /**
     * Return the table's `table` tag inline style
     *
     * @since 5.2.0
     *
     * @return string
     */
    protected function get_table_table_style() {
        return sprintf(
            'border-collapse:collapse;color:%1$s;font-family:%2$s;font-size:%3$s;Margin-bottom:15px;text-align:left;width:100%%;',
            $this->get_css( 'font-color', false ),
            $this->get_css( 'font-family', false ),
            $this->get_css( 'font-size', false )
        );
    }
 
    /**
     * Return the table's `tr` tag inline style
     *
     * @since 5.2.0
     *
     * @return string
     */
    protected function get_table_tr_style() {
        return 'color:inherit;font-family:inherit;font-size:inherit;';
    }
 
    /**
     * Return the table's `td` tag inline style
     *
     * @since 5.2.0
     *
     * @return string
     */
    protected function get_table_td_style() {
        return sprintf(
            'border-bottom:1px solid %s;color:inherit;font-family:inherit;font-size:inherit;padding:10px;',
            $this->get_css( 'divider-color', false )
        );
    }
 
    /**
     * Returns the table html
     *
     * @since 5.2.0
     *
     * @param array $rows Array of rows to populate the table with.
     * @return string
     */
    public function get_table_html( $rows ) {
 
        if ( empty( $rows ) ) {
            return '';
        }
 
        list( $table_style, $tr_style, $td_style ) = $this->get_parsed_table_style();
 
        ob_start();
        ?>
        <table style="<?php echo $table_style; ?>">
        <?php foreach ( $rows as $code => $name ) : ?>
            <tr style="<?php echo $tr_style; ?>">
                <th style="<?php echo $td_style; ?>width:33.3333%;"><?php echo $name; ?></th>
                <td style="<?php echo $td_style; ?>">{{<?php echo $code; ?>}}</td>
            </tr>
        <?php endforeach; ?>
        </table>
        <?php
 
        return ob_get_clean();
 
    }
 
}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Removed the deprecated LLMS_Emails::$_instance property.
5.3.0 Replace singleton code with LLMS_Trait_Singleton.
3.8.0 Unknown.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.