Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
74.00% covered (warning)
74.00%
37 / 50
50.00% covered (danger)
50.00%
5 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Post_Settings
74.00% covered (warning)
74.00%
37 / 50
50.00% covered (danger)
50.00%
5 / 10
43.78
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_settings
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
4.59
 update_setting
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 is_enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_custom_text
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_image_type
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 get_image_url
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
9.01
 get_template
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 get_font
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_token
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * PostSettings class.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize\Social_Image_Generator;
9
10use Automattic\Jetpack\Publicize\Publicize;
11
12/**
13 * This class is used to get SIG-specific information from a post.
14 */
15class Post_Settings {
16    /**
17     * Post to get information from.
18     *
19     * @var int $post_id
20     */
21    public $post_id;
22
23    /**
24     * The post's settings.
25     *
26     * @var array $settings;
27     */
28    public $settings;
29
30    /**
31     * Constructor.
32     *
33     * @param int $post_id Post to get information from.
34     */
35    public function __construct( $post_id ) {
36        $this->post_id  = $post_id;
37        $this->settings = $this->get_settings();
38    }
39
40    /**
41     * Get the SIG settings.
42     *
43     * @param bool $raw Whether to get the raw settings, skipping the defaults.
44     * @return array
45     */
46    public function get_settings( $raw = false ) {
47        $social_options = $raw
48            ? get_metadata_raw( 'post', $this->post_id, Publicize::POST_JETPACK_SOCIAL_OPTIONS, true )
49            : get_post_meta( $this->post_id, Publicize::POST_JETPACK_SOCIAL_OPTIONS, true );
50
51        if ( ! is_array( $social_options ) || empty( $social_options['image_generator_settings'] ) ) {
52            return array();
53        }
54
55        return $social_options['image_generator_settings'];
56    }
57
58    /**
59     * Update a SIG setting.
60     *
61     * @param string $key The key to update.
62     * @param mixed  $value The value to set for the key.
63     * @return bool True if the update was successful.
64     */
65    public function update_setting( $key, $value ) {
66        $social_options = get_post_meta( $this->post_id, Publicize::POST_JETPACK_SOCIAL_OPTIONS, true );
67
68        if ( empty( $social_options ) ) {
69            $social_options = array();
70        }
71
72        $updated_options = array_replace_recursive( $social_options, array( 'image_generator_settings' => array( $key => $value ) ) );
73
74        $updated = update_post_meta( $this->post_id, Publicize::POST_JETPACK_SOCIAL_OPTIONS, $updated_options );
75
76        if ( $updated ) {
77            $this->settings = $updated_options['image_generator_settings'];
78        }
79
80        return (bool) $updated;
81    }
82
83    /**
84     * Check if SIG is enabled for a specific post.
85     *
86     * @return bool
87     */
88    public function is_enabled() {
89        return ! empty( $this->settings['enabled'] );
90    }
91
92    /**
93     * Get the text to use for the generated image.
94     *
95     * @return string
96     */
97    public function get_custom_text() {
98        if ( ! empty( $this->settings['custom_text'] ) ) {
99            return $this->settings['custom_text'];
100        }
101
102        return get_the_title( $this->post_id );
103    }
104
105    /**
106     * Get the image type for the generated image.
107     *
108     * @return string
109     */
110    public function get_image_type() {
111        $type              = isset( $this->settings['image_type'] ) ? $this->settings['image_type'] : null;
112        $featured_image_id = get_post_thumbnail_id( $this->post_id );
113
114        // By default, we use the featured image.
115        if ( empty( $type ) ) {
116            if ( ! empty( $featured_image_id ) ) {
117                return 'featured';
118            } else {
119                return 'default';
120            }
121        }
122
123        return $type;
124    }
125
126    /**
127     * Get the image to use for the generated image.
128     *
129     * @return ?string
130     */
131    public function get_image_url() {
132        $type = $this->get_image_type();
133
134        switch ( $type ) {
135            case 'featured':
136                $image_id = get_post_thumbnail_id( $this->post_id );
137                break;
138            case 'custom':
139                $image_id = isset( $this->settings['image_id'] ) ? $this->settings['image_id'] : null;
140                break;
141            case 'none':
142                return null;
143            case 'default':
144                $image_id = isset( $this->settings['default_image_id'] ) ? $this->settings['default_image_id'] : null;
145                break;
146        }
147
148        if ( empty( $image_id ) ) {
149            return null;
150        }
151
152        $url = wp_get_attachment_image_url( $image_id, 'large' );
153
154        if ( ! $url ) {
155            return null;
156        }
157
158        return $url;
159    }
160
161    /**
162     * Get the template to use for the generated image.
163     *
164     * @return string
165     */
166    public function get_template() {
167        if ( ! empty( $this->settings['template'] ) ) {
168            return $this->settings['template'];
169        }
170
171        return Templates::DEFAULT_TEMPLATE;
172    }
173
174    /**
175     * Get the font to use for the text in the generated image.
176     *
177     * @return string
178     */
179    public function get_font() {
180        return $this->settings['font'] ?? '';
181    }
182
183    /**
184     * Get an image token.
185     *
186     * @return string
187     */
188    public function get_token() {
189        return ! empty( $this->settings['token'] ) ? $this->settings['token'] : '';
190    }
191}