options_helper = $options_helper; } /** * Stores the values for the site representation. * * @param array $params The values to store. * * @return object The response object. */ public function set_site_representation( $params ) { $failures = []; foreach ( self::SITE_REPRESENTATION_FIELDS as $field_name ) { if ( isset( $params[ $field_name ] ) ) { if ( $field_name === 'description' && \current_user_can( 'manage_options' ) ) { $result = \update_option( 'blogdescription', $params['description'] ); if ( ! $result && $params['description'] === \get_option( 'blogdescription' ) ) { $result = true; } } else { $result = $this->options_helper->set( $field_name, $params[ $field_name ] ); } if ( ! $result ) { $failures[] = $field_name; } } } if ( \count( $failures ) === 0 ) { return (object) [ 'success' => true, 'status' => 200, ]; } return (object) [ 'success' => false, 'status' => 500, 'error' => 'Could not save some options in the database', 'failures' => $failures, ]; } /** * Stores the values for the social profiles. * * @param array $params The values to store. * * @return object The response object. */ public function set_social_profiles( $params ) { $failures = []; foreach ( self::SOCIAL_PROFILES_FIELDS as $field_name ) { if ( isset( $params[ $field_name ] ) ) { $result = $this->options_helper->set( $field_name, $params[ $field_name ] ); if ( ! $result ) { /** * The value for Twitter might have been sanitised from URL to username. * If so, $result will be false. We should check if the option value is part of the received value. */ if ( $field_name === 'twitter_site' ) { $current_option = $this->options_helper->get( $field_name ); if ( ! \strpos( $params[ $field_name ], 'twitter.com/' . $current_option ) ) { $failures[] = $field_name; } } else { $failures[] = $field_name; } } } } if ( \count( $failures ) === 0 ) { return (object) [ 'success' => true, 'status' => 200, ]; } return (object) [ 'success' => false, 'status' => 500, 'error' => 'Could not save some options in the database', 'failures' => $failures, ]; } /** * Stores the values to enable/disable tracking. * * @param array $params The values to store. * * @return object The response object. */ public function set_enable_tracking( $params ) { $success = true; $option_value = $this->options_helper->get( 'tracking' ); if ( $option_value !== $params['tracking'] ) { $success = $this->options_helper->set( 'tracking', $params['tracking'] ); } if ( $success ) { return (object) [ 'success' => true, 'status' => 200, ]; } return (object) [ 'success' => false, 'status' => 500, 'error' => 'Could not save the option in the database', ]; } }