WPSEO_Option_Social

Option: wpseo_social.

Defined (1)

The class is defined in the following location(s).

/inc/options/class-wpseo-option-social.php  
  1. class WPSEO_Option_Social extends WPSEO_Option { 
  2.  
  3. /** 
  4. * @var string option name 
  5. */ 
  6. public $option_name = 'wpseo_social'; 
  7.  
  8. /** 
  9. * @var array Array of defaults for the option 
  10. * Shouldn't be requested directly, use $this->get_defaults(); 
  11. */ 
  12. protected $defaults = array( 
  13. // Non-form fields, set via procedural code in admin/pages/social.php. 
  14. 'fb_admins' => array(), // Array of user id's => array( name => '', link => '' ). 
  15.  
  16. // Non-form field, set via translate_defaults() and validate_option() methods. 
  17. 'fbconnectkey' => '',  
  18. // Form fields: 
  19. 'facebook_site' => '', // Text field. 
  20. 'instagram_url' => '',  
  21. 'linkedin_url' => '',  
  22. 'myspace_url' => '',  
  23. 'og_default_image' => '', // Text field. 
  24. 'og_frontpage_title' => '', // Text field. 
  25. 'og_frontpage_desc' => '', // Text field. 
  26. 'og_frontpage_image' => '', // Text field. 
  27. 'opengraph' => true,  
  28. 'pinterest_url' => '',  
  29. 'pinterestverify' => '',  
  30. 'plus-publisher' => '', // Text field. 
  31. 'twitter' => true,  
  32. 'twitter_site' => '', // Text field. 
  33. 'twitter_card_type' => 'summary',  
  34. 'youtube_url' => '',  
  35. 'google_plus_url' => '',  
  36. // Form field, but not always available: 
  37. 'fbadminapp' => '', // Facebook app ID. 
  38. ); 
  39.  
  40. /** 
  41. * @var array Array of sub-options which should not be overloaded with multi-site defaults 
  42. */ 
  43. public $ms_exclude = array( 
  44. /** privacy */ 
  45. 'fb_admins',  
  46. 'fbconnectkey',  
  47. 'fbadminapp',  
  48. 'pinterestverify',  
  49. ); 
  50.  
  51.  
  52. /** 
  53. * @var array Array of allowed twitter card types 
  54. * While we only have the options summary and summary_large_image in the 
  55. * interface now, we might change that at some point. 
  56. * @internal Uncomment any of these to allow them in validation *and* automatically add them as a choice 
  57. * in the options page 
  58. */ 
  59. public static $twitter_card_types = array( 
  60. 'summary' => '',  
  61. 'summary_large_image' => '',  
  62. // 'photo' => '',  
  63. // 'gallery' => '',  
  64. // 'app' => '',  
  65. // 'player' => '',  
  66. // 'product' => '',  
  67. ); 
  68.  
  69.  
  70. /** 
  71. * Get the singleton instance of this class 
  72. * @return object 
  73. */ 
  74. public static function get_instance() { 
  75. if ( ! ( self::$instance instanceof self ) ) { 
  76. self::$instance = new self(); 
  77.  
  78. return self::$instance; 
  79.  
  80.  
  81. /** 
  82. * Translate/set strings used in the option defaults 
  83. * @return void 
  84. */ 
  85. public function translate_defaults() { 
  86. /** Auto-magically set the fb connect key */ 
  87. $this->defaults['fbconnectkey'] = self::get_fbconnectkey(); 
  88.  
  89. self::$twitter_card_types['summary'] = __( 'Summary', 'wordpress-seo' ); 
  90. self::$twitter_card_types['summary_large_image'] = __( 'Summary with large image', 'wordpress-seo' ); 
  91.  
  92.  
  93. /** 
  94. * Get a Facebook connect key for the blog 
  95. * @static 
  96. * @return string 
  97. */ 
  98. public static function get_fbconnectkey() { 
  99. return md5( get_bloginfo( 'url' ) . rand() ); 
  100.  
  101.  
  102. /** 
  103. * Validate the option 
  104. * @param array $dirty New value for the option. 
  105. * @param array $clean Clean value for the option, normally the defaults. 
  106. * @param array $old Old value of the option. 
  107. * @return array Validated clean value for the option to be saved to the database 
  108. */ 
  109. protected function validate_option( $dirty, $clean, $old ) { 
  110.  
  111. foreach ( $clean as $key => $value ) { 
  112. switch ( $key ) { 
  113. /** Automagic Facebook connect key */ 
  114. case 'fbconnectkey': 
  115. if ( ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) && preg_match( '`^[a-f0-9]{32}$`', $old[ $key ] ) > 0 ) { 
  116. $clean[ $key ] = $old[ $key ]; 
  117. else { 
  118. $clean[ $key ] = self::get_fbconnectkey(); 
  119. break; 
  120.  
  121.  
  122. /** Will not always exist in form */ 
  123. case 'fb_admins': 
  124. if ( isset( $dirty[ $key ] ) && is_array( $dirty[ $key ] ) ) { 
  125. if ( $dirty[ $key ] === array() ) { 
  126. $clean[ $key ] = array(); 
  127. else { 
  128. foreach ( $dirty[ $key ] as $user_id => $fb_array ) { 
  129. /** 
  130. * @todo [JRF/JRF => Yoast/whomever] add user_id validation - 
  131. * are these WP user-ids or FB user-ids ? Probably FB user-ids,  
  132. * if so, find out the rules for FB user-ids 
  133. */ 
  134. if ( is_array( $fb_array ) && $fb_array !== array() ) { 
  135. foreach ( $fb_array as $fb_key => $fb_value ) { 
  136. switch ( $fb_key ) { 
  137. case 'name': 
  138. /** 
  139. * @todo [JRF => whomever] add validation for name based 
  140. * on rules if there are any 
  141. * Input comes from: $_GET['userrealname'] 
  142. */ 
  143. $clean[ $key ][ $user_id ][ $fb_key ] = sanitize_text_field( $fb_value ); 
  144. break; 
  145.  
  146. case 'link': 
  147. $clean[ $key ][ $user_id ][ $fb_key ] = WPSEO_Utils::sanitize_url( $fb_value ); 
  148. break; 
  149. unset( $user_id, $fb_array, $fb_key, $fb_value ); 
  150. elseif ( isset( $old[ $key ] ) && is_array( $old[ $key ] ) ) { 
  151. $clean[ $key ] = $old[ $key ]; 
  152. break; 
  153.  
  154. /** text fields */ 
  155. case 'og_frontpage_desc': 
  156. case 'og_frontpage_title': 
  157. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) { 
  158. $clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] ); 
  159. break; 
  160.  
  161.  
  162. /** url text fields - no ftp allowed */ 
  163. case 'facebook_site': 
  164. case 'instagram_url': 
  165. case 'linkedin_url': 
  166. case 'myspace_url': 
  167. case 'pinterest_url': 
  168. case 'plus-publisher': 
  169. case 'og_default_image': 
  170. case 'og_frontpage_image': 
  171. case 'youtube_url': 
  172. case 'google_plus_url': 
  173. $this->validate_url( $key, $dirty, $old, $clean ); 
  174. break; 
  175.  
  176. case 'pinterestverify': 
  177. $this->validate_verification_string( $key, $dirty, $old, $clean ); 
  178. break; 
  179.  
  180. /** twitter user name */ 
  181. case 'twitter_site': 
  182. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) { 
  183. $twitter_id = sanitize_text_field( ltrim( $dirty[ $key ], '@' ) ); 
  184. /** 
  185. * From the Twitter documentation about twitter screen names: 
  186. * Typically a maximum of 15 characters long, but some historical accounts 
  187. * may exist with longer names. 
  188. * A username can only contain alphanumeric characters (letters A-Z, numbers 0-9) 
  189. * with the exception of underscores 
  190. * @link https://support.twitter.com/articles/101299-why-can-t-i-register-certain-usernames 
  191. * @link https://dev.twitter.com/docs/platform-objects/users 
  192. */ 
  193. if ( preg_match( '`^[A-Za-z0-9_]{1, 25}$`', $twitter_id ) ) { 
  194. $clean[ $key ] = $twitter_id; 
  195. elseif ( preg_match( '`^http(?:s)?://(?:www\.)?twitter\.com/(?P<handle>[A-Za-z0-9_]{1, 25})/?$`', $twitter_id, $matches ) ) { 
  196. $clean[ $key ] = $matches['handle']; 
  197. else { 
  198. if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) { 
  199. $twitter_id = sanitize_text_field( ltrim( $old[ $key ], '@' ) ); 
  200. if ( preg_match( '`^[A-Za-z0-9_]{1, 25}$`', $twitter_id ) ) { 
  201. $clean[ $key ] = $twitter_id; 
  202. if ( function_exists( 'add_settings_error' ) ) { 
  203. add_settings_error( 
  204. $this->group_name, // Slug title of the setting. 
  205. '_' . $key, // Suffix-id for the error message box. 
  206. sprintf( __( '%s does not seem to be a valid Twitter user-id. Please correct.', 'wordpress-seo' ), '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>' ), // The error message. 
  207. 'error' // Error type, either 'error' or 'updated'. 
  208. ); 
  209. unset( $twitter_id ); 
  210. break; 
  211.  
  212. case 'twitter_card_type': 
  213. if ( isset( $dirty[ $key ], self::$twitter_card_types[ $dirty[ $key ] ] ) && $dirty[ $key ] !== '' ) { 
  214. $clean[ $key ] = $dirty[ $key ]; 
  215. break; 
  216.  
  217. /** boolean fields */ 
  218. case 'opengraph': 
  219. case 'twitter': 
  220. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false ); 
  221. break; 
  222.  
  223. /** 
  224. * Only validate 'fbadminapp', so leave the clean default. 
  225. */ 
  226. if ( isset( $dirty['fbadminapp'] ) && ! empty( $dirty['fbadminapp'] ) ) { 
  227. $clean['fbadminapp'] = $dirty['fbadminapp']; 
  228.  
  229.  
  230. return $clean; 
  231.  
  232.  
  233. /** 
  234. * Clean a given option value 
  235. * @param array $option_value Old (not merged with defaults or filtered) option value to 
  236. * clean according to the rules for this option. 
  237. * @param string $current_version (optional) Version from which to upgrade, if not set,  
  238. * version specific upgrades will be disregarded. 
  239. * @param array $all_old_option_values (optional) Only used when importing old options to have 
  240. * access to the real old values, in contrast to the saved ones. 
  241. * @return array Cleaned option 
  242. */ 
  243. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) { 
  244.  
  245. /** Move options from very old option to this one */ 
  246. $old_option = null; 
  247. if ( isset( $all_old_option_values ) ) { 
  248. // Ok, we have an import. 
  249. if ( isset( $all_old_option_values['wpseo_indexation'] ) && is_array( $all_old_option_values['wpseo_indexation'] ) && $all_old_option_values['wpseo_indexation'] !== array() ) { 
  250. $old_option = $all_old_option_values['wpseo_indexation']; 
  251. else { 
  252. $old_option = get_option( 'wpseo_indexation' ); 
  253.  
  254. if ( is_array( $old_option ) && $old_option !== array() ) { 
  255. $move = array( 
  256. 'opengraph',  
  257. 'fb_adminid',  
  258. 'fb_appid',  
  259. ); 
  260. foreach ( $move as $key ) { 
  261. if ( isset( $old_option[ $key ] ) && ! isset( $option_value[ $key ] ) ) { 
  262. $option_value[ $key ] = $old_option[ $key ]; 
  263. unset( $move, $key ); 
  264. unset( $old_option ); 
  265.  
  266.  
  267. return $option_value;