| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Configuration; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Configuration_Workout_Action. |
| 9 |
*/ |
| 10 |
class Configuration_Workout_Action { |
| 11 |
|
| 12 |
/** |
| 13 |
* The Options_Helper instance. |
| 14 |
* |
| 15 |
* @var Options_Helper |
| 16 |
*/ |
| 17 |
protected $options_helper; |
| 18 |
|
| 19 |
/** |
| 20 |
* The fields for the site representation payload. |
| 21 |
*/ |
| 22 |
const SITE_REPRESENTATION_FIELDS = [ |
| 23 |
'company_or_person', |
| 24 |
'company_name', |
| 25 |
'company_logo', |
| 26 |
'company_logo_id', |
| 27 |
'person_logo', |
| 28 |
'person_logo_id', |
| 29 |
'company_or_person_user_id', |
| 30 |
'description', |
| 31 |
]; |
| 32 |
|
| 33 |
/** |
| 34 |
* The fields for the social profiles payload. |
| 35 |
*/ |
| 36 |
const SOCIAL_PROFILES_FIELDS = [ |
| 37 |
'facebook_site', |
| 38 |
'twitter_site', |
| 39 |
'instagram_url', |
| 40 |
'linkedin_url', |
| 41 |
'myspace_url', |
| 42 |
'pinterest_url', |
| 43 |
'youtube_url', |
| 44 |
'wikipedia_url', |
| 45 |
]; |
| 46 |
|
| 47 |
/** |
| 48 |
* Configuration_Workout_Action constructor. |
| 49 |
* |
| 50 |
* @param Options_Helper $options_helper The WPSEO options helper. |
| 51 |
*/ |
| 52 |
public function __construct( Options_Helper $options_helper ) { |
| 53 |
$this->options_helper = $options_helper; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Stores the values for the site representation. |
| 58 |
* |
| 59 |
* @param array $params The values to store. |
| 60 |
* |
| 61 |
* @return object The response object. |
| 62 |
*/ |
| 63 |
public function set_site_representation( $params ) { |
| 64 |
$failures = []; |
| 65 |
|
| 66 |
foreach ( self::SITE_REPRESENTATION_FIELDS as $field_name ) { |
| 67 |
if ( isset( $params[ $field_name ] ) ) { |
| 68 |
if ( $field_name === 'description' && \current_user_can( 'manage_options' ) ) { |
| 69 |
$result = \update_option( 'blogdescription', $params['description'] ); |
| 70 |
if ( ! $result && $params['description'] === \get_option( 'blogdescription' ) ) { |
| 71 |
$result = true; |
| 72 |
} |
| 73 |
} |
| 74 |
else { |
| 75 |
$result = $this->options_helper->set( $field_name, $params[ $field_name ] ); |
| 76 |
} |
| 77 |
if ( ! $result ) { |
| 78 |
$failures[] = $field_name; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if ( \count( $failures ) === 0 ) { |
| 84 |
return (object) [ |
| 85 |
'success' => true, |
| 86 |
'status' => 200, |
| 87 |
]; |
| 88 |
} |
| 89 |
return (object) [ |
| 90 |
'success' => false, |
| 91 |
'status' => 500, |
| 92 |
'error' => 'Could not save some options in the database', |
| 93 |
'failures' => $failures, |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Stores the values for the social profiles. |
| 99 |
* |
| 100 |
* @param array $params The values to store. |
| 101 |
* |
| 102 |
* @return object The response object. |
| 103 |
*/ |
| 104 |
public function set_social_profiles( $params ) { |
| 105 |
$failures = []; |
| 106 |
|
| 107 |
foreach ( self::SOCIAL_PROFILES_FIELDS as $field_name ) { |
| 108 |
if ( isset( $params[ $field_name ] ) ) { |
| 109 |
$result = $this->options_helper->set( $field_name, $params[ $field_name ] ); |
| 110 |
if ( ! $result ) { |
| 111 |
/** |
| 112 |
* The value for Twitter might have been sanitised from URL to username. |
| 113 |
* If so, $result will be false. We should check if the option value is part of the received value. |
| 114 |
*/ |
| 115 |
if ( $field_name === 'twitter_site' ) { |
| 116 |
$current_option = $this->options_helper->get( $field_name ); |
| 117 |
if ( ! \strpos( $params[ $field_name ], 'twitter.com/' . $current_option ) ) { |
| 118 |
$failures[] = $field_name; |
| 119 |
} |
| 120 |
} |
| 121 |
else { |
| 122 |
$failures[] = $field_name; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if ( \count( $failures ) === 0 ) { |
| 129 |
return (object) [ |
| 130 |
'success' => true, |
| 131 |
'status' => 200, |
| 132 |
]; |
| 133 |
} |
| 134 |
return (object) [ |
| 135 |
'success' => false, |
| 136 |
'status' => 500, |
| 137 |
'error' => 'Could not save some options in the database', |
| 138 |
'failures' => $failures, |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Stores the values to enable/disable tracking. |
| 144 |
* |
| 145 |
* @param array $params The values to store. |
| 146 |
* |
| 147 |
* @return object The response object. |
| 148 |
*/ |
| 149 |
public function set_enable_tracking( $params ) { |
| 150 |
$success = true; |
| 151 |
$option_value = $this->options_helper->get( 'tracking' ); |
| 152 |
|
| 153 |
if ( $option_value !== $params['tracking'] ) { |
| 154 |
$success = $this->options_helper->set( 'tracking', $params['tracking'] ); |
| 155 |
} |
| 156 |
|
| 157 |
if ( $success ) { |
| 158 |
return (object) [ |
| 159 |
'success' => true, |
| 160 |
'status' => 200, |
| 161 |
]; |
| 162 |
} |
| 163 |
return (object) [ |
| 164 |
'success' => false, |
| 165 |
'status' => 500, |
| 166 |
'error' => 'Could not save the option in the database', |
| 167 |
]; |
| 168 |
} |
| 169 |
} |
| 170 |
|