Yoast_Social_Facebook

The Facebook insights class, this will add some listeners to fetch GET params.

Defined (1)

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

/admin/class-social-facebook.php  
  1. class Yoast_Social_Facebook { 
  2.  
  3. /** 
  4. * @var array - The options for social 
  5. */ 
  6. private $options; 
  7.  
  8. /** 
  9. * @var Yoast_Social_Facebook_Form 
  10. */ 
  11. private $form; 
  12.  
  13. /** 
  14. * Setting the options and define the listener to fetch $_GET values 
  15. */ 
  16. public function __construct() { 
  17. $this->options = get_option( 'wpseo_social' ); 
  18.  
  19. $this->get_listener(); 
  20.  
  21. $this->form = new Yoast_Social_Facebook_Form(); 
  22.  
  23. /** 
  24. * Returns the output from the form class 
  25. */ 
  26. public function show_form() { 
  27. $this->form->show_form(); 
  28.  
  29. /** 
  30. * Adding a new admin 
  31. * @param string $admin_name Name string. 
  32. * @param string $admin_id ID string. 
  33. * @return string 
  34. */ 
  35. public function add_admin( $admin_name, $admin_id ) { 
  36. $success = 0; 
  37.  
  38. // If one of the fields is empty. 
  39. if ( empty( $admin_name ) || empty( $admin_id ) ) { 
  40. $response_body = $this->get_response_body( 'not_present' ); 
  41. else { 
  42. $admin_id = $this->parse_admin_id( $admin_id ); 
  43.  
  44. if ( ! isset( $this->options['fb_admins'][ $admin_id ] ) ) { 
  45. $name = sanitize_text_field( urldecode( $admin_name ) ); 
  46. $admin_id = sanitize_text_field( $admin_id ); 
  47.  
  48. if ( preg_match( '/[0-9]+?/', $admin_id ) && preg_match( '/[\w\s]+?/', $name ) ) { 
  49. $this->options['fb_admins'][ $admin_id ]['name'] = $name; 
  50. $this->options['fb_admins'][ $admin_id ]['link'] = urldecode( 'http://www.facebook.com/' . $admin_id ); 
  51.  
  52. $this->save_options(); 
  53.  
  54. $success = 1; 
  55. $response_body = $this->form->get_admin_link( $admin_id, $this->options['fb_admins'][ $admin_id ] ); 
  56. else { 
  57. $response_body = $this->get_response_body( 'invalid_format' ); 
  58. else { 
  59. $response_body = $this->get_response_body( 'already_exists' ); 
  60.  
  61. return wp_json_encode( 
  62. array( 
  63. 'success' => $success,  
  64. 'html' => $response_body,  
  65. ); 
  66.  
  67. /** 
  68. * Fetches the id if the full meta tag or a full url was given 
  69. * @param string $admin_id Admin ID input string to process. 
  70. * @return string 
  71. */ 
  72. private function parse_admin_id( $admin_id ) { 
  73. if ( preg_match( '/^\<meta property\=\"fb:admins\" content\=\"(\d+?)\"/', $admin_id, $matches_full_meta ) ) { 
  74. return $matches_full_meta[1]; 
  75.  
  76. return trim( parse_url( $admin_id, PHP_URL_PATH ), '/' ); 
  77.  
  78. /** 
  79. * Returns a different response body depending on the response type 
  80. * @param string $type Type string. 
  81. * @return string 
  82. */ 
  83. private function get_response_body( $type ) { 
  84. switch ( $type ) { 
  85. case 'not_present': 
  86. $return = "<p class='notice-error notice'><span>" . __( 'Please make sure both fields are filled.', 'wordpress-seo' ) . '</span></p>'; 
  87. break; 
  88. case 'invalid_format': 
  89. $return = "<p class='notice-error notice'><span>" . __( 'Your input contains invalid characters. Please make sure both fields are filled in correctly.', 'wordpress-seo' ) . '</span></p>'; 
  90. break; 
  91. case 'already_exists': 
  92. $return = "<p class='notice-error notice'><span>" . __( 'This Facebook user has already been added as an admin.', 'wordpress-seo' ) . '</span></p>'; 
  93. break; 
  94. default: 
  95. $return = ''; 
  96. break; 
  97.  
  98. return $return; 
  99.  
  100. /** 
  101. * This method will hook into the defined get params 
  102. */ 
  103. private function get_listener() { 
  104. if ( $delfbadmin = filter_input( INPUT_GET, 'delfbadmin' ) ) { 
  105. $this->delete_admin( $delfbadmin ); 
  106. elseif ( filter_input( INPUT_GET, 'fbclearall' ) ) { 
  107. $this->clear_all(); 
  108.  
  109. /** 
  110. * Deletes the admin from the options 
  111. * @param string $delfbadmin Facebook admin ID. 
  112. */ 
  113. private function delete_admin( $delfbadmin ) { 
  114. $this->verify_nonce( 'delfbadmin' ); 
  115.  
  116. $admin_id = sanitize_text_field( $delfbadmin ); 
  117. if ( isset( $this->options['fb_admins'][ $admin_id ] ) ) { 
  118. $fbadmin = $this->options['fb_admins'][ $admin_id ]['name']; 
  119. unset( $this->options['fb_admins'][ $admin_id ] ); 
  120.  
  121. $this->save_options(); 
  122. $this->success_notice( sprintf( __( 'Successfully removed admin %s', 'wordpress-seo' ), $fbadmin ) ); 
  123.  
  124. unset( $fbadmin ); 
  125.  
  126. unset( $admin_id ); 
  127.  
  128. // Clean up the referrer url for later use. 
  129. if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { 
  130. $this->cleanup_referrer_url( 'nonce', 'delfbadmin' ); 
  131.  
  132. /** 
  133. * Clear all the facebook that has been set already 
  134. */ 
  135. private function clear_all() { 
  136. $this->verify_nonce( 'fbclearall' ); 
  137.  
  138. // Reset to defaults, don't unset as otherwise the old values will be retained. 
  139. $this->options['fb_admins'] = WPSEO_Options::get_default( 'wpseo_social', 'fb_admins' ); 
  140.  
  141. $this->save_options(); 
  142. $this->success_notice( __( 'Successfully cleared all Facebook Data', 'wordpress-seo' ) ); 
  143.  
  144. // Clean up the referrer url for later use. 
  145. if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { 
  146. $this->cleanup_referrer_url( 'nonce', 'fbclearall' ); 
  147.  
  148. /** 
  149. * Clean up the request_uri. The given params are the params that will be removed from the URL 
  150. */ 
  151. private function cleanup_referrer_url() { 
  152. $_SERVER['REQUEST_URI'] = remove_query_arg( 
  153. func_get_args(),  
  154. sanitize_text_field( $_SERVER['REQUEST_URI'] ) 
  155. ); 
  156.  
  157. /** 
  158. * When something is going well, show a success notice 
  159. * @param string $notice_text Message string. 
  160. */ 
  161. private function success_notice( $notice_text ) { 
  162. add_settings_error( 'yoast_wpseo_social_options', 'success', $notice_text, 'updated' ); 
  163.  
  164. /** 
  165. * Verify the nonce from the URL with the saved nonce 
  166. * @param string $nonce_name Nonce name string. 
  167. */ 
  168. private function verify_nonce( $nonce_name ) { 
  169. if ( wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), $nonce_name ) != 1 ) { 
  170. die( "I don't think that's really nice of you!." ); 
  171.  
  172. /** 
  173. * Saving the options 
  174. */ 
  175. private function save_options() { 
  176. update_option( 'wpseo_social', $this->options );