Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.85% covered (warning)
78.85%
41 / 52
70.00% covered (warning)
70.00%
7 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Post_Settings
78.85% covered (warning)
78.85%
41 / 52
70.00% covered (warning)
70.00%
7 / 10
33.90
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%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 get_image_type
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 get_image_url
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
7.01
 get_template
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_font
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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        /**
99         * Filter to disable the text overlay on Social Image Generator templates.
100         *
101         * @module publicize
102         *
103         * @since $$next-version$$
104         *
105         * @param bool $disable_text Whether to disable the text overlay. Default false.
106         * @param int  $post_id      The Post ID.
107         */
108        if ( apply_filters( 'jetpack_social_image_generator_disable_text', false, $this->post_id ) ) {
109            return '';
110        }
111
112        if ( ! empty( $this->settings['custom_text'] ) ) {
113            return $this->settings['custom_text'];
114        }
115
116        return get_the_title( $this->post_id );
117    }
118
119    /**
120     * Get the image type for the generated image.
121     *
122     * @return string
123     */
124    public function get_image_type() {
125        $type              = $this->settings['image_type'] ?? null;
126        $featured_image_id = get_post_thumbnail_id( $this->post_id );
127
128        // By default, we use the featured image.
129        if ( empty( $type ) ) {
130            if ( ! empty( $featured_image_id ) ) {
131                return 'featured';
132            } else {
133                return 'default';
134            }
135        }
136
137        return $type;
138    }
139
140    /**
141     * Get the image to use for the generated image.
142     *
143     * @return ?string
144     */
145    public function get_image_url() {
146        $type = $this->get_image_type();
147
148        switch ( $type ) {
149            case 'featured':
150                $image_id = get_post_thumbnail_id( $this->post_id );
151                break;
152            case 'custom':
153                $image_id = $this->settings['image_id'] ?? null;
154                break;
155            case 'none':
156                return null;
157            case 'default':
158                $image_id = $this->settings['default_image_id'] ?? null;
159                break;
160        }
161
162        if ( empty( $image_id ) ) {
163            return null;
164        }
165
166        $url = wp_get_attachment_image_url( $image_id, 'large' );
167
168        if ( ! $url ) {
169            return null;
170        }
171
172        return $url;
173    }
174
175    /**
176     * Get the template to use for the generated image.
177     *
178     * @return string
179     */
180    public function get_template() {
181        if ( ! empty( $this->settings['template'] ) ) {
182            return $this->settings['template'];
183        }
184
185        return Templates::DEFAULT_TEMPLATE;
186    }
187
188    /**
189     * Get the font to use for the text in the generated image.
190     *
191     * @return string
192     */
193    public function get_font() {
194        return $this->settings['font'] ?? '';
195    }
196
197    /**
198     * Get an image token.
199     *
200     * @return string
201     */
202    public function get_token() {
203        return ! empty( $this->settings['token'] ) ? $this->settings['token'] : '';
204    }
205}