PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/class-social-facebook.php

https://gitlab.com/bhargavi_dcw/dflocal
PHP | 482 lines | 264 code | 65 blank | 153 comment | 23 complexity | 25ef9fd385c2fa929388a92a7f0c22a0 MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO
  4. * @subpackage Admin
  5. */
  6. /**
  7. * The Facebook insights class, this will add some listeners to fetch GET params
  8. */
  9. class Yoast_Social_Facebook {
  10. /**
  11. * @var array - The options for social
  12. */
  13. private $options;
  14. /**
  15. * @var Yoast_Social_Facebook_Form
  16. */
  17. private $form;
  18. /**
  19. * Setting the options and define the listener to fetch $_GET values
  20. */
  21. public function __construct() {
  22. $this->options = get_option( 'wpseo_social' );
  23. $this->get_listener();
  24. $this->form = new Yoast_Social_Facebook_Form();
  25. }
  26. /**
  27. * Returns the output from the form class
  28. */
  29. public function show_form() {
  30. $this->form->show_form();
  31. }
  32. /**
  33. * Adding a new admin
  34. *
  35. * @param string $admin_name Name string.
  36. * @param string $admin_id ID string.
  37. *
  38. * @return string
  39. */
  40. public function add_admin( $admin_name, $admin_id ) {
  41. $success = 0;
  42. // If one of the fields is empty.
  43. if ( empty( $admin_name ) || empty( $admin_id ) ) {
  44. $response_body = $this->get_response_body( 'not_present' );
  45. }
  46. else {
  47. $admin_id = $this->parse_admin_id( $admin_id );
  48. if ( ! isset( $this->options['fb_admins'][ $admin_id ] ) ) {
  49. $name = sanitize_text_field( urldecode( $admin_name ) );
  50. $admin_id = sanitize_text_field( $admin_id );
  51. if ( preg_match( '/[0-9]+?/', $admin_id ) && preg_match( '/[\w\s]+?/', $name ) ) {
  52. $this->options['fb_admins'][ $admin_id ]['name'] = $name;
  53. $this->options['fb_admins'][ $admin_id ]['link'] = urldecode( 'http://www.facebook.com/' . $admin_id );
  54. $this->save_options();
  55. $success = 1;
  56. $response_body = $this->form->get_admin_link( $admin_id, $this->options['fb_admins'][ $admin_id ] );
  57. }
  58. else {
  59. $response_body = $this->get_response_body( 'invalid_format' );
  60. }
  61. }
  62. else {
  63. $response_body = $this->get_response_body( 'already_exists' );
  64. }
  65. }
  66. return wp_json_encode(
  67. array(
  68. 'success' => $success,
  69. 'html' => $response_body,
  70. )
  71. );
  72. }
  73. /**
  74. * Fetches the id if the full meta tag or a full url was given
  75. *
  76. * @param string $admin_id Admin ID input string to process.
  77. *
  78. * @return string
  79. */
  80. private function parse_admin_id( $admin_id ) {
  81. if ( preg_match( '/^\<meta property\=\"fb:admins\" content\=\"(\d+?)\"/', $admin_id, $matches_full_meta ) ) {
  82. return $matches_full_meta[1];
  83. }
  84. return trim( parse_url( $admin_id, PHP_URL_PATH ), '/' );
  85. }
  86. /**
  87. * Returns a different response body depending on the response type
  88. *
  89. * @param string $type Type string.
  90. *
  91. * @return string
  92. */
  93. private function get_response_body( $type ) {
  94. switch ( $type ) {
  95. case 'not_present':
  96. $return = "<p class='notice-error notice'><span style='margin-left: 5px'>" . __( 'Please make sure both fields are filled.', 'wordpress-seo' ) . '</span></p>';
  97. break;
  98. case 'invalid_format':
  99. $return = "<p class='notice-error notice'><span style='margin-left: 5px'>" . __( 'Your input contains invalid characters. Please make sure both fields are filled in correctly.', 'wordpress-seo' ) . '</span></p>';
  100. break;
  101. case 'already_exists':
  102. $return = "<p class='notice-error notice'><span style='margin-left: 5px'>" . __( 'This Facebook user has already been added as an admin.', 'wordpress-seo' ) . '</span></p>';
  103. break;
  104. default:
  105. $return = '';
  106. break;
  107. }
  108. return $return;
  109. }
  110. /**
  111. * This method will hook into the defined get params
  112. */
  113. private function get_listener() {
  114. if ( $delfbadmin = filter_input( INPUT_GET, 'delfbadmin' ) ) {
  115. $this->delete_admin( $delfbadmin );
  116. }
  117. elseif ( filter_input( INPUT_GET, 'fbclearall' ) ) {
  118. $this->clear_all();
  119. }
  120. }
  121. /**
  122. * Deletes the admin from the options
  123. *
  124. * @param string $delfbadmin Facebook admin ID.
  125. */
  126. private function delete_admin( $delfbadmin ) {
  127. $this->verify_nonce( 'delfbadmin' );
  128. $admin_id = sanitize_text_field( $delfbadmin );
  129. if ( isset( $this->options['fb_admins'][ $admin_id ] ) ) {
  130. $fbadmin = $this->options['fb_admins'][ $admin_id ]['name'];
  131. unset( $this->options['fb_admins'][ $admin_id ] );
  132. $this->save_options();
  133. $this->success_notice( sprintf( __( 'Successfully removed admin %s', 'wordpress-seo' ), $fbadmin ) );
  134. unset( $fbadmin );
  135. }
  136. unset( $admin_id );
  137. // Clean up the referrer url for later use.
  138. if ( filter_input( INPUT_SERVER, 'REQUEST_URI' ) ) {
  139. $this->cleanup_referrer_url( 'nonce', 'delfbadmin' );
  140. }
  141. }
  142. /**
  143. * Clear all the facebook that has been set already
  144. */
  145. private function clear_all() {
  146. $this->verify_nonce( 'fbclearall' );
  147. // Reset to defaults, don't unset as otherwise the old values will be retained.
  148. $this->options['fb_admins'] = WPSEO_Options::get_default( 'wpseo_social', 'fb_admins' );
  149. $this->save_options();
  150. $this->success_notice( __( 'Successfully cleared all Facebook Data', 'wordpress-seo' ) );
  151. // Clean up the referrer url for later use.
  152. if ( filter_input( INPUT_SERVER, 'REQUEST_URI' ) ) {
  153. $this->cleanup_referrer_url( 'nonce', 'fbclearall' );
  154. }
  155. }
  156. /**
  157. * Clean up the request_uri. The given params are the params that will be removed from the URL
  158. */
  159. private function cleanup_referrer_url() {
  160. $_SERVER['REQUEST_URI'] = remove_query_arg(
  161. func_get_args(),
  162. filter_input(
  163. INPUT_SERVER, 'REQUEST_URI', FILTER_CALLBACK, array( 'options' => 'sanitize_text_field' )
  164. )
  165. );
  166. }
  167. /**
  168. * When something is going well, show a success notice
  169. *
  170. * @param string $notice_text Message string.
  171. */
  172. private function success_notice( $notice_text ) {
  173. add_settings_error( 'yoast_wpseo_social_options', 'success', $notice_text, 'updated' );
  174. }
  175. /**
  176. * Verify the nonce from the URL with the saved nonce
  177. *
  178. * @param string $nonce_name Nonce name string.
  179. */
  180. private function verify_nonce( $nonce_name ) {
  181. if ( wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), $nonce_name ) != 1 ) {
  182. die( "I don't think that's really nice of you!." );
  183. }
  184. }
  185. /**
  186. * Saving the options
  187. */
  188. private function save_options() {
  189. update_option( 'wpseo_social', $this->options );
  190. }
  191. }
  192. /**
  193. * This will display the HTML for the facebook insights part
  194. */
  195. class Yoast_Social_Facebook_Form {
  196. /**
  197. * @var array - The options for social
  198. */
  199. private $options;
  200. /**
  201. * @var array - The repository for the buttons that will be shown
  202. */
  203. private $buttons = array();
  204. /**
  205. * @var string - The URL to link to
  206. */
  207. private $admin_url = 'admin.php?page=wpseo_social';
  208. /**
  209. * Setting the options and call the methods to display everything
  210. */
  211. public function __construct() {
  212. $this->options = get_option( 'wpseo_social' );
  213. }
  214. /**
  215. * Returns the output-property
  216. */
  217. public function show_form() {
  218. $this
  219. ->form_head()
  220. ->manage_user_admin()
  221. ->form_thickbox()
  222. ->show_buttons()
  223. ->manage_app_as_admin();
  224. }
  225. /**
  226. * Parses the admin_link
  227. *
  228. * @param string $admin_id Facebook admin ID string.
  229. * @param array $admin Admin data array.
  230. * @param string|bool $nonce Optional nonce string.
  231. *
  232. * @return string
  233. */
  234. public function get_admin_link( $admin_id, $admin, $nonce = false ) {
  235. if ( $nonce === false ) {
  236. $nonce = $this->get_delete_nonce();
  237. }
  238. $return = '<li><a target="_blank" href="' . esc_url( $admin['link'] ) . '">' . esc_html( $admin['name'] ) . '</a>';
  239. $return .= ' - <strong><a href="' . $this->admin_delete_link( $admin_id, $nonce ) . '">X</a></strong></li>';
  240. return $return;
  241. }
  242. /**
  243. * SHow the top of the social insights part of the page
  244. *
  245. * @return $this
  246. */
  247. private function form_head() {
  248. echo '<h2>' . esc_html__( 'Facebook Insights and Admins', 'wordpress-seo' ) . '</h2>';
  249. echo '<p>', sprintf(
  250. /* translators: %1$s and %2$s expand to a link to Facebook Insights */
  251. esc_html__( 'To be able to access %1$sFacebook Insights%2$s for your site, you need to specify a Facebook Admin. This can be a user. If you have an app for your site, you could use that as well.', 'wordpress-seo' ),
  252. '<a target="_blank" href="https://www.facebook.com/insights">',
  253. '</a>'
  254. );
  255. echo ' ';
  256. /* translators: %1$s and %2$s expand to a link to the Yoast Knowledge Base */
  257. printf( __( 'More info can be found %1$son our knowledge base%2$s.', 'wordpress-seo' ), '<a target="_blank" href="http://kb.yoast.com/article/254-gaining-access-to-facebook-insights">', '</a>' );
  258. echo '</p>';
  259. return $this;
  260. }
  261. /**
  262. * Show the form inside the thickbox
  263. */
  264. private function form_thickbox() {
  265. // Adding the thickbox.
  266. add_thickbox();
  267. echo '<div id="add_facebook_admin" style="display:none;">';
  268. echo "<div class='form-wrap wpseo_content_wrapper'>";
  269. echo '<p>';
  270. /* translators: %1$s and %2$s expand to a link to Facebook Insights */
  271. printf( __( 'To be able to access %1$sFacebook Insights%2$s, you need to add a user here. The name is used for reference only, the ID is used for verification.', 'wordpress-seo' ), '<a target="_blank" href="https://www.facebook.com/insights">', '</a>' );
  272. echo '</p>';
  273. echo '<p>';
  274. /* translators: %1$s and %2$s expand to a link to the Yoast Knowledge Base */
  275. printf( __( 'If you don\'t know where to find the needed ID, see %1$sthis knowledge base article%2$s.', 'wordpress-seo' ), '<a target="_blank" href="http://kb.yoast.com/article/254-gaining-access-to-facebook-insights">', '</a>' );
  276. echo '</p>';
  277. echo '<div class="form-field form-required">';
  278. echo '<label for="fb_admin_name">' . __( 'Admin\'s name:', 'wordpress-seo' ) . '</label>';
  279. echo '<input type="text" id="fb_admin_name" name="fb_admin_name" value="" maxlength="255" />';
  280. echo '</div>';
  281. echo '<div class="form-field form-required">';
  282. echo '<label for="fb_admin_id">' . __( 'Admin\'s Facebook user ID:', 'wordpress-seo' ) . '</label>';
  283. echo '<input type="text" id="fb_admin_id" name="fb_admin_id" value="" maxlength="255" />';
  284. echo '</div>';
  285. echo "<p class='submit'>";
  286. echo '<input type="hidden" name="fb_admin_nonce" value="' . wp_create_nonce( 'wpseo_fb_admin_nonce' ) . '" />';
  287. echo '<input type="submit" value="' . __( 'Add Facebook admin', 'wordpress-seo' ) . '" class="button button-primary" onclick="javascript:wpseo_add_fb_admin();" />';
  288. echo '</p>';
  289. echo '</div>';
  290. echo '</div>';
  291. return $this;
  292. }
  293. /**
  294. * Display the buttons to add an admin or add another admin from Facebook and display the admin that has been added already.
  295. *
  296. * @return $this
  297. */
  298. private function manage_user_admin() {
  299. $button_text = __( 'Add Facebook admin', 'wordpress-seo' );
  300. $nonce = false;
  301. $style = 'style="display:none"';
  302. if ( is_array( $this->options['fb_admins'] ) && $this->options['fb_admins'] !== array() ) {
  303. $nonce = $this->get_delete_nonce();
  304. $button_text = __( 'Add Another Facebook Admin', 'wordpress-seo' );
  305. $style = '';
  306. }
  307. echo "<div id='connected_fb_admins' {$style}>";
  308. echo '<p>' . __( 'Currently connected Facebook admins:', 'wordpress-seo' ) . '</p>';
  309. echo '<ul id="user_admin">';
  310. $this->show_user_admins( $nonce );
  311. echo '</ul>';
  312. echo '</div>';
  313. unset( $nonce );
  314. $this->add_button(
  315. array(
  316. 'url' => '#TB_inline?width=600&height=350&inlineId=add_facebook_admin',
  317. 'value' => $button_text,
  318. 'class' => 'thickbox',
  319. 'title' => $button_text,
  320. )
  321. );
  322. return $this;
  323. }
  324. /**
  325. * Show input field to set a facebook apps as an admin
  326. *
  327. * @return $this
  328. */
  329. private function manage_app_as_admin() {
  330. echo '<div class="clear"></div><br />';
  331. Yoast_Form::get_instance()->textinput( 'fbadminapp', __( 'Facebook App ID', 'wordpress-seo' ) );
  332. return $this;
  333. }
  334. /**
  335. * Loop through the fb-admins to parse the output for them
  336. *
  337. * @param string $nonce Nonce string.
  338. */
  339. private function show_user_admins( $nonce ) {
  340. foreach ( $this->options['fb_admins'] as $admin_id => $admin ) {
  341. echo $this->get_admin_link( $admin_id, $admin, $nonce );
  342. }
  343. }
  344. /**
  345. * Parsing the link that directs to the admin removal
  346. *
  347. * @param string $admin_id Facebook admin ID.
  348. * @param string $nonce Nonce string.
  349. *
  350. * @return string
  351. */
  352. private function admin_delete_link( $admin_id, $nonce ) {
  353. return esc_url(
  354. add_query_arg(
  355. array(
  356. 'delfbadmin' => esc_attr( $admin_id ),
  357. 'nonce' => $nonce,
  358. ),
  359. admin_url( $this->admin_url . '#top#facebook' )
  360. )
  361. );
  362. }
  363. /**
  364. * Adding a button to the button property
  365. *
  366. * @param array $args Arguments data array.
  367. */
  368. private function add_button( $args ) {
  369. $args = wp_parse_args(
  370. $args,
  371. array(
  372. 'url' => '',
  373. 'value' => '',
  374. 'class' => '',
  375. 'id' => '',
  376. 'title' => '',
  377. )
  378. );
  379. $this->buttons[] = '<a title="' . esc_attr( $args['title'] ) . '" id="' . esc_attr( $args['id'] ) . '" class="button' . ' ' . esc_attr( $args['class'] ) . '" href="' . esc_url( $args['url'] ) . '">' . esc_html( $args['value'] ) . '</a>';
  380. }
  381. /**
  382. * Showing the buttons
  383. */
  384. private function show_buttons() {
  385. if ( $this->get_clearall() ) {
  386. $this->add_button(
  387. array(
  388. 'url' => add_query_arg( array(
  389. 'nonce' => wp_create_nonce( 'fbclearall' ),
  390. 'fbclearall' => 'true',
  391. ), admin_url( $this->admin_url . '#top#facebook' ) ),
  392. 'value' => __( 'Clear all Facebook Data', 'wordpress-seo' ),
  393. )
  394. );
  395. }
  396. if ( is_array( $this->buttons ) && $this->buttons !== array() ) {
  397. echo '<p class="fb-buttons">' . implode( '', $this->buttons ) . '</p>';
  398. }
  399. return $this;
  400. }
  401. /**
  402. * Check if the clear button should be displayed. This is based on the set options.
  403. *
  404. * @return bool
  405. */
  406. private function get_clearall() {
  407. return is_array( $this->options['fb_admins'] ) && $this->options['fb_admins'] !== array();
  408. }
  409. /**
  410. * Creates nonce for removal link
  411. *
  412. * @return mixed
  413. */
  414. private function get_delete_nonce() {
  415. return wp_create_nonce( 'delfbadmin' );
  416. }
  417. }