| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Introductions\Infrastructure; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Yoast\WP\SEO\Helpers\User_Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Takes care of the get/set in the WP user meta. |
| 10 |
* |
| 11 |
* @makePublic |
| 12 |
*/ |
| 13 |
class Wistia_Embed_Permission_Repository { |
| 14 |
|
| 15 |
public const USER_META_KEY = '_yoast_wpseo_wistia_embed_permission'; |
| 16 |
|
| 17 |
public const DEFAULT_VALUE = false; |
| 18 |
|
| 19 |
/** |
| 20 |
* Holds the User_Helper instance. |
| 21 |
* |
| 22 |
* @var User_Helper |
| 23 |
*/ |
| 24 |
private $user_helper; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructs the class. |
| 28 |
* |
| 29 |
* @param User_Helper $user_helper The User_Helper. |
| 30 |
*/ |
| 31 |
public function __construct( User_Helper $user_helper ) { |
| 32 |
$this->user_helper = $user_helper; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Retrieves the current value for a user. |
| 37 |
* |
| 38 |
* @param int $user_id User ID. |
| 39 |
* |
| 40 |
* @return bool The current value. |
| 41 |
* |
| 42 |
* @throws Exception If an invalid user ID is supplied. |
| 43 |
*/ |
| 44 |
public function get_value_for_user( $user_id ) { |
| 45 |
$value = $this->user_helper->get_meta( $user_id, self::USER_META_KEY, true ); |
| 46 |
if ( $value === false ) { |
| 47 |
throw new Exception( 'Invalid User ID' ); |
| 48 |
} |
| 49 |
|
| 50 |
if ( $value === '0' || $value === '1' ) { |
| 51 |
// The value is stored as a string because otherwise we can not see the difference between false and an invalid user ID. |
| 52 |
return $value === '1'; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Why could $value be invalid? |
| 57 |
* - When the database row does not exist yet, $value can be an empty string. |
| 58 |
* - Faulty data was stored? |
| 59 |
*/ |
| 60 |
return self::DEFAULT_VALUE; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Sets the Wistia embed permission value for the current user. |
| 65 |
* |
| 66 |
* @param int $user_id The user ID. |
| 67 |
* @param bool $value The value. |
| 68 |
* |
| 69 |
* @return bool Whether the update was successful. |
| 70 |
* |
| 71 |
* @throws Exception If an invalid user ID is supplied. |
| 72 |
*/ |
| 73 |
public function set_value_for_user( $user_id, $value ) { |
| 74 |
// The value is stored as a string because otherwise we can not see the difference between false and an invalid user ID. |
| 75 |
$value_as_string = ( $value === true ) ? '1' : '0'; |
| 76 |
|
| 77 |
// Checking for only false, not interested in not having to update. |
| 78 |
return $this->user_helper->update_meta( $user_id, self::USER_META_KEY, $value_as_string ) !== false; |
| 79 |
} |
| 80 |
} |
| 81 |
|