Handler.php
2 weeks ago
Post_Thumbnail_Setter.php
2 weeks ago
Queue.php
2 weeks ago
Tester.php
2 weeks ago
Post_Thumbnail_Setter.php
177 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Tribe__Process__Post_Thumbnail_Setter |
| 5 | * |
| 6 | * Handles upload and setting of a post thumbnail in an async process. |
| 7 | * Example usage: |
| 8 | * |
| 9 | * $post_thumbnail_setter = new Tribe__Process__Post_Thumbnail_Setter(); |
| 10 | * $post_thumbnail_setter->set_post_id( $post_id ); |
| 11 | * $post_thumbnail_setter->set_post_thumbnail( 'http://foo.com/random-image.jpg' ); |
| 12 | * $post_thumbnail_setter->dispatch(); |
| 13 | * |
| 14 | * @since 4.7.12 |
| 15 | */ |
| 16 | class Tribe__Process__Post_Thumbnail_Setter extends Tribe__Process__Handler { |
| 17 | /** |
| 18 | * @var int The ID of the post the post thumbnail should be assigned to. |
| 19 | */ |
| 20 | protected $post_id; |
| 21 | |
| 22 | /** |
| 23 | * @var int|string Either the ID of an attachment that should be set as the post thumbnail |
| 24 | * or the full URL, or file path, to it. |
| 25 | */ |
| 26 | protected $post_thumbnail; |
| 27 | |
| 28 | /** |
| 29 | * {@inheritdoc} |
| 30 | */ |
| 31 | public static function action() { |
| 32 | return 'post_thumbnail_setter'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * {@inheritdoc} |
| 37 | */ |
| 38 | public function dispatch() { |
| 39 | if ( ! isset( $this->post_id, $this->post_thumbnail ) ) { |
| 40 | // since this is a developer error we are not localizing this error string |
| 41 | throw new InvalidArgumentException( 'Post ID and featured image should be set before trying to dispatch.' ); |
| 42 | } |
| 43 | |
| 44 | $data = [ |
| 45 | 'post_id' => $this->post_id, |
| 46 | 'post_thumbnail' => trim( $this->post_thumbnail ), |
| 47 | ]; |
| 48 | |
| 49 | $this->data( $data ); |
| 50 | |
| 51 | do_action( 'tribe_log', 'debug', $this->identifier, $data ); |
| 52 | |
| 53 | return parent::dispatch(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets the ID of the post the post thumbnail (aka "featured image") should be attached |
| 58 | * and set for. |
| 59 | * |
| 60 | * @since 4.7.12 |
| 61 | * |
| 62 | * @param int $post_id The target post ID. |
| 63 | */ |
| 64 | public function set_post_id( $post_id ) { |
| 65 | $this->post_id = $post_id; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sets the post thumbnail ID or source the process should set. |
| 70 | * |
| 71 | * @since 4.7.12 |
| 72 | * |
| 73 | * @param int|string $post_thumbnail Either an attachment ID or the full URL, or path, to |
| 74 | * the post thumbnail image. |
| 75 | */ |
| 76 | public function set_post_thumbnail( $post_thumbnail ) { |
| 77 | $this->post_thumbnail = $post_thumbnail; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Handles the post thumbnail setting async process. |
| 82 | * |
| 83 | * The post thumbnail will be uploaded, if not uploaded already, using the `tribe_upload_image` function. |
| 84 | * This method is an alias of the publicly accessible `sync_handle` one. |
| 85 | * |
| 86 | * @since 4.7.12 |
| 87 | * |
| 88 | * @param array|null $data_source An optional source of data. |
| 89 | * |
| 90 | * @see Tribe__Process__Post_Thumbnail_Setter::sync_handle() |
| 91 | * |
| 92 | * @see tribe_upload_image() |
| 93 | */ |
| 94 | protected function handle( array $data_source = null ) { |
| 95 | $this->sync_handle( $data_source ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * {@inheritdoc} |
| 100 | */ |
| 101 | public function sync_handle( array $data_source = null ) { |
| 102 | do_action( 'tribe_log', 'debug', $this->identifier, [ 'status' => 'handling request' ] ); |
| 103 | |
| 104 | $data_source = isset( $data_source ) ? $data_source : $_POST; |
| 105 | |
| 106 | if ( ! isset( $data_source['post_id'], $data_source['post_thumbnail'] ) ) { |
| 107 | do_action( 'tribe_log', 'error', $this->identifier, [ 'data' => $data_source, ] ); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | $id = filter_var( $data_source['post_id'], FILTER_SANITIZE_NUMBER_INT ); |
| 113 | $post_thumbnail = filter_var( $data_source['post_thumbnail'], FILTER_SANITIZE_STRING ); |
| 114 | |
| 115 | do_action( 'tribe_log', 'debug', $this->identifier, [ |
| 116 | 'status' => 'fetching thumbnail', |
| 117 | 'post_thumbnail' => $post_thumbnail, |
| 118 | 'post_id' => $id, |
| 119 | ] ); |
| 120 | |
| 121 | $thumbnail_id = tribe_upload_image( $post_thumbnail ); |
| 122 | |
| 123 | if ( false === $thumbnail_id ) { |
| 124 | do_action( |
| 125 | 'tribe_log', |
| 126 | 'error', |
| 127 | $this->identifier, |
| 128 | [ |
| 129 | 'action' => 'fetch', |
| 130 | 'post_thumbnail' => $post_thumbnail, |
| 131 | 'post_id' => $id, |
| 132 | 'status' => 'could not fetch', |
| 133 | ] |
| 134 | ); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | $set = true; |
| 140 | if ( (int) get_post_thumbnail_id( $id ) !== (int) $thumbnail_id ) { |
| 141 | $set = set_post_thumbnail( $id, $thumbnail_id ); |
| 142 | } |
| 143 | |
| 144 | if ( false === $set ) { |
| 145 | do_action( |
| 146 | 'tribe_log', |
| 147 | 'error', |
| 148 | $this->identifier, |
| 149 | [ |
| 150 | 'action' => 'set', |
| 151 | 'post_thumbnail' => $post_thumbnail, |
| 152 | 'attachment_id' => $thumbnail_id, |
| 153 | 'post_id' => $id, |
| 154 | 'status' => 'unable to set thumbnail', |
| 155 | ] |
| 156 | ); |
| 157 | |
| 158 | return $thumbnail_id; |
| 159 | } |
| 160 | |
| 161 | do_action( |
| 162 | 'tribe_log', |
| 163 | 'debug', |
| 164 | $this->identifier, |
| 165 | [ |
| 166 | 'action' => 'set', |
| 167 | 'post_thumbnail' => $post_thumbnail, |
| 168 | 'attachment_id' => $thumbnail_id, |
| 169 | 'post_id' => $id, |
| 170 | 'status' => 'completed - attachment created and linked to the post', |
| 171 | ] |
| 172 | ); |
| 173 | |
| 174 | return $thumbnail_id; |
| 175 | } |
| 176 | } |
| 177 |