PageRenderTime 33ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/wp-content/plugins/jetpack/modules/widgets/twitter-timeline.php

https://gitlab.com/vanafroo/voipWEB
PHP | 470 lines | 342 code | 51 blank | 77 comment | 28 complexity | 203bee67570d67c03ce723e1a4f6e073 MD5 | raw file
  1. <?php
  2. /*
  3. * Based on Evolution Twitter Timeline
  4. * (https://wordpress.org/extend/plugins/evolution-twitter-timeline/)
  5. * For details on Twitter Timelines see:
  6. * - https://twitter.com/settings/widgets
  7. * - https://dev.twitter.com/docs/embedded-timelines
  8. */
  9. /**
  10. * Register the widget for use in Appearance -> Widgets
  11. */
  12. add_action( 'widgets_init', 'jetpack_twitter_timeline_widget_init' );
  13. function jetpack_twitter_timeline_widget_init() {
  14. register_widget( 'Jetpack_Twitter_Timeline_Widget' );
  15. }
  16. class Jetpack_Twitter_Timeline_Widget extends WP_Widget {
  17. /**
  18. * Register widget with WordPress.
  19. */
  20. public function __construct() {
  21. parent::__construct(
  22. 'twitter_timeline',
  23. /** This filter is documented in modules/widgets/facebook-likebox.php */
  24. apply_filters( 'jetpack_widget_name', esc_html__( 'Twitter Timeline', 'jetpack' ) ),
  25. array(
  26. 'classname' => 'widget_twitter_timeline',
  27. 'description' => __( 'Display an official Twitter Embedded Timeline widget.', 'jetpack' ),
  28. 'customize_selective_refresh' => true,
  29. )
  30. );
  31. if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
  32. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  33. }
  34. add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
  35. }
  36. /**
  37. * Enqueue scripts.
  38. */
  39. public function enqueue_scripts() {
  40. wp_enqueue_script( 'jetpack-twitter-timeline' );
  41. }
  42. /**
  43. * Enqueue Twitter's widget library.
  44. *
  45. * @deprecated
  46. */
  47. public function library() {
  48. _deprecated_function( __METHOD__, '4.0.0' );
  49. wp_print_scripts( array( 'jetpack-twitter-timeline' ) );
  50. }
  51. /**
  52. * Enqueue script to improve admin UI
  53. */
  54. public function admin_scripts( $hook ) {
  55. // This is still 'widgets.php' when managing widgets via the Customizer.
  56. if ( 'widgets.php' === $hook ) {
  57. wp_enqueue_script( 'twitter-timeline-admin', plugins_url( 'twitter-timeline-admin.js', __FILE__ ) );
  58. }
  59. }
  60. /**
  61. * Front-end display of widget.
  62. *
  63. * @see WP_Widget::widget()
  64. *
  65. * @param array $args Widget arguments.
  66. * @param array $instance Saved values from database.
  67. */
  68. public function widget( $args, $instance ) {
  69. $instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 );
  70. echo $args['before_widget'];
  71. if ( isset( $instance['title'] ) ) {
  72. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  73. echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
  74. }
  75. // Start tag output
  76. // This tag is transformed into the widget markup by Twitter's
  77. // widgets.js code
  78. echo '<a class="twitter-timeline"';
  79. $data_attribs = array(
  80. 'width',
  81. 'height',
  82. 'theme',
  83. 'link-color',
  84. 'border-color',
  85. 'tweet-limit',
  86. 'lang'
  87. );
  88. foreach ( $data_attribs as $att ) {
  89. if ( ! empty( $instance[ $att ] ) && ! is_array( $instance[ $att ] ) ) {
  90. echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[ $att ] ) . '"';
  91. }
  92. }
  93. if ( ! empty( $instance['chrome'] ) && is_array( $instance['chrome'] ) ) {
  94. echo ' data-chrome="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"';
  95. }
  96. $type = ( isset( $instance['type'] ) ? $instance['type'] : '' );
  97. $widget_id = ( isset( $instance['widget-id'] ) ? $instance['widget-id'] : '' );
  98. switch ( $type ) {
  99. case 'profile':
  100. echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
  101. break;
  102. case 'widget-id':
  103. default:
  104. echo ' data-widget-id="' . esc_attr( $widget_id ) . '"';
  105. break;
  106. }
  107. // End tag output
  108. echo '>';
  109. $timeline_placeholder = __( 'My Tweets', 'jetpack' );
  110. /**
  111. * Filter the Timeline placeholder text.
  112. *
  113. * @module widgets
  114. *
  115. * @since 3.4.0
  116. *
  117. * @param string $timeline_placeholder Timeline placeholder text.
  118. */
  119. $timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder );
  120. echo esc_html( $timeline_placeholder ) . '</a>';
  121. // End tag output
  122. echo $args['after_widget'];
  123. /** This action is documented in modules/widgets/social-media-icons.php */
  124. do_action( 'jetpack_bump_stats_extras', 'widget', 'twitter_timeline' );
  125. }
  126. /**
  127. * Sanitize widget form values as they are saved.
  128. *
  129. * @see WP_Widget::update()
  130. *
  131. * @param array $new_instance Values just sent to be saved.
  132. * @param array $old_instance Previously saved values from database.
  133. *
  134. * @return array Updated safe values to be saved.
  135. */
  136. public function update( $new_instance, $old_instance ) {
  137. $instance = array();
  138. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  139. $width = (int) $new_instance['width'];
  140. if ( $width ) {
  141. // From publish.twitter.com: 220 <= width <= 1200
  142. $instance['width'] = min( max( $width, 220 ), 1200 );
  143. } else {
  144. $instance['width'] = '';
  145. }
  146. $height = (int) $new_instance['height'];
  147. if ( $height ) {
  148. // From publish.twitter.com: height >= 200
  149. $instance['height'] = max( $height, 200 );
  150. } else {
  151. $instance['height'] = '';
  152. }
  153. $tweet_limit = (int) $new_instance['tweet-limit'];
  154. $instance['tweet-limit'] = ( $tweet_limit ? $tweet_limit : null );
  155. // If they entered something that might be a full URL, try to parse it out
  156. if ( is_string( $new_instance['widget-id'] ) ) {
  157. if ( preg_match(
  158. '#https?://twitter\.com/settings/widgets/(\d+)#s',
  159. $new_instance['widget-id'],
  160. $matches
  161. ) ) {
  162. $new_instance['widget-id'] = $matches[1];
  163. }
  164. }
  165. $instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] );
  166. $hex_regex = '/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/';
  167. foreach ( array( 'link-color', 'border-color' ) as $color ) {
  168. $new_color = sanitize_text_field( $new_instance[ $color ] );
  169. if ( preg_match( $hex_regex, $new_color ) ) {
  170. $instance[ $color ] = $new_color;
  171. }
  172. }
  173. $instance['type'] = 'widget-id';
  174. if ( in_array( $new_instance['type'], array( 'widget-id', 'profile' ) ) ) {
  175. $instance['type'] = $new_instance['type'];
  176. }
  177. $instance['theme'] = 'light';
  178. if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) {
  179. $instance['theme'] = $new_instance['theme'];
  180. }
  181. $instance['chrome'] = array();
  182. $chrome_settings = array(
  183. 'noheader',
  184. 'nofooter',
  185. 'noborders',
  186. 'transparent'
  187. );
  188. if ( isset( $new_instance['chrome'] ) ) {
  189. foreach ( $new_instance['chrome'] as $chrome ) {
  190. if ( in_array( $chrome, $chrome_settings ) ) {
  191. $instance['chrome'][] = $chrome;
  192. }
  193. }
  194. }
  195. return $instance;
  196. }
  197. /**
  198. * Returns a link to the documentation for a feature of this widget on
  199. * Jetpack or WordPress.com.
  200. */
  201. public function get_docs_link( $hash = '' ) {
  202. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  203. $base_url = 'https://support.wordpress.com/widgets/twitter-timeline-widget/';
  204. } else {
  205. $base_url = 'https://jetpack.com/support/extra-sidebar-widgets/twitter-timeline-widget/';
  206. }
  207. return '<a href="' . $base_url . $hash . '" target="_blank">( ? )</a>';
  208. }
  209. /**
  210. * Back-end widget form.
  211. *
  212. * @see WP_Widget::form()
  213. *
  214. * @param array $instance Previously saved values from database.
  215. */
  216. public function form( $instance ) {
  217. $defaults = array(
  218. 'title' => esc_html__( 'Follow me on Twitter', 'jetpack' ),
  219. 'width' => '',
  220. 'height' => '400',
  221. 'widget-id' => '',
  222. 'link-color' => '#f96e5b',
  223. 'border-color' => '#e8e8e8',
  224. 'theme' => 'light',
  225. 'chrome' => array(),
  226. 'tweet-limit' => null,
  227. );
  228. $instance = wp_parse_args( (array) $instance, $defaults );
  229. if ( empty( $instance['type'] ) ) {
  230. // Decide the correct widget type. If this is a pre-existing
  231. // widget with a numeric widget ID, then the type should be
  232. // 'widget-id', otherwise it should be 'profile'.
  233. if ( ! empty( $instance['widget-id'] ) && is_numeric( $instance['widget-id'] ) ) {
  234. $instance['type'] = 'widget-id';
  235. } else {
  236. $instance['type'] = 'profile';
  237. }
  238. }
  239. ?>
  240. <p>
  241. <label for="<?php echo $this->get_field_id( 'title' ); ?>">
  242. <?php esc_html_e( 'Title:', 'jetpack' ); ?>
  243. </label>
  244. <input
  245. class="widefat"
  246. id="<?php echo $this->get_field_id( 'title' ); ?>"
  247. name="<?php echo $this->get_field_name( 'title' ); ?>"
  248. type="text"
  249. value="<?php echo esc_attr( $instance['title'] ); ?>"
  250. />
  251. </p>
  252. <p>
  253. <label for="<?php echo $this->get_field_id( 'width' ); ?>">
  254. <?php esc_html_e( 'Maximum Width (px; 220 to 1200):', 'jetpack' ); ?>
  255. </label>
  256. <input
  257. class="widefat"
  258. id="<?php echo $this->get_field_id( 'width' ); ?>"
  259. name="<?php echo $this->get_field_name( 'width' ); ?>"
  260. type="number" min="220" max="1200"
  261. value="<?php echo esc_attr( $instance['width'] ); ?>"
  262. />
  263. </p>
  264. <p>
  265. <label for="<?php echo $this->get_field_id( 'height' ); ?>">
  266. <?php esc_html_e( 'Height (px; at least 200):', 'jetpack' ); ?>
  267. </label>
  268. <input
  269. class="widefat"
  270. id="<?php echo $this->get_field_id( 'height' ); ?>"
  271. name="<?php echo $this->get_field_name( 'height' ); ?>"
  272. type="number" min="200"
  273. value="<?php echo esc_attr( $instance['height'] ); ?>"
  274. />
  275. </p>
  276. <p>
  277. <label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>">
  278. <?php esc_html_e( '# of Tweets Shown:', 'jetpack' ); ?>
  279. </label>
  280. <input
  281. class="widefat"
  282. id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"
  283. name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>"
  284. type="number" min="1" max="20"
  285. value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>"
  286. />
  287. </p>
  288. <p class="jetpack-twitter-timeline-widget-type-container">
  289. <label for="<?php echo $this->get_field_id( 'type' ); ?>">
  290. <?php esc_html_e( 'Widget Type:', 'jetpack' ); ?>
  291. <?php echo $this->get_docs_link( '#widget-type' ); ?>
  292. </label>
  293. <select
  294. name="<?php echo $this->get_field_name( 'type' ); ?>"
  295. id="<?php echo $this->get_field_id( 'type' ); ?>"
  296. class="jetpack-twitter-timeline-widget-type widefat"
  297. >
  298. <option value="profile"<?php selected( $instance['type'], 'profile' ); ?>>
  299. <?php esc_html_e( 'Profile', 'jetpack' ); ?>
  300. </option>
  301. <option value="widget-id"<?php selected( $instance['type'], 'widget-id' ); ?>>
  302. <?php esc_html_e( 'Widget ID', 'jetpack' ); ?>
  303. </option>
  304. </select>
  305. </p>
  306. <p class="jetpack-twitter-timeline-widget-id-container">
  307. <label
  308. for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
  309. data-widget-type="widget-id"
  310. <?php echo ( 'widget-id' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
  311. >
  312. <?php esc_html_e( 'Widget ID:', 'jetpack' ); ?>
  313. <?php echo $this->get_docs_link( '#widget-id' ); ?>
  314. </label>
  315. <label
  316. for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
  317. data-widget-type="profile"
  318. <?php echo ( 'profile' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
  319. >
  320. <?php esc_html_e( 'Twitter Username:', 'jetpack' ); ?>
  321. <?php echo $this->get_docs_link( '#twitter-username' ); ?>
  322. </label>
  323. <input
  324. class="widefat"
  325. id="<?php echo $this->get_field_id( 'widget-id' ); ?>"
  326. name="<?php echo $this->get_field_name( 'widget-id' ); ?>"
  327. type="text"
  328. value="<?php echo esc_attr( $instance['widget-id'] ); ?>"
  329. />
  330. </p>
  331. <p>
  332. <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
  333. <?php esc_html_e( 'Layout Options:', 'jetpack' ); ?>
  334. </label>
  335. <br />
  336. <input
  337. type="checkbox"<?php checked( in_array( 'noheader', $instance['chrome'] ) ); ?>
  338. id="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"
  339. name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
  340. value="noheader"
  341. />
  342. <label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
  343. <?php esc_html_e( 'No Header', 'jetpack' ); ?>
  344. </label>
  345. <br />
  346. <input
  347. type="checkbox"<?php checked( in_array( 'nofooter', $instance['chrome'] ) ); ?>
  348. id="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>"
  349. name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
  350. value="nofooter"
  351. />
  352. <label for="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>">
  353. <?php esc_html_e( 'No Footer', 'jetpack' ); ?>
  354. </label>
  355. <br />
  356. <input
  357. type="checkbox"<?php checked( in_array( 'noborders', $instance['chrome'] ) ); ?>
  358. id="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>"
  359. name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
  360. value="noborders"
  361. />
  362. <label for="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>">
  363. <?php esc_html_e( 'No Borders', 'jetpack' ); ?>
  364. </label>
  365. <br />
  366. <input
  367. type="checkbox"<?php checked( in_array( 'transparent', $instance['chrome'] ) ); ?>
  368. id="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>"
  369. name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
  370. value="transparent"
  371. />
  372. <label for="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>">
  373. <?php esc_html_e( 'Transparent Background', 'jetpack' ); ?>
  374. </label>
  375. </p>
  376. <p>
  377. <label for="<?php echo $this->get_field_id( 'link-color' ); ?>">
  378. <?php _e( 'Link Color (hex):', 'jetpack' ); ?>
  379. </label>
  380. <input
  381. class="widefat"
  382. id="<?php echo $this->get_field_id( 'link-color' ); ?>"
  383. name="<?php echo $this->get_field_name( 'link-color' ); ?>"
  384. type="text"
  385. value="<?php echo esc_attr( $instance['link-color'] ); ?>"
  386. />
  387. </p>
  388. <p>
  389. <label for="<?php echo $this->get_field_id( 'border-color' ); ?>">
  390. <?php _e( 'Border Color (hex):', 'jetpack' ); ?>
  391. </label>
  392. <input
  393. class="widefat"
  394. id="<?php echo $this->get_field_id( 'border-color' ); ?>"
  395. name="<?php echo $this->get_field_name( 'border-color' ); ?>"
  396. type="text"
  397. value="<?php echo esc_attr( $instance['border-color'] ); ?>"
  398. />
  399. </p>
  400. <p>
  401. <label for="<?php echo $this->get_field_id( 'theme' ); ?>">
  402. <?php _e( 'Timeline Theme:', 'jetpack' ); ?>
  403. </label>
  404. <select
  405. name="<?php echo $this->get_field_name( 'theme' ); ?>"
  406. id="<?php echo $this->get_field_id( 'theme' ); ?>"
  407. class="widefat"
  408. >
  409. <option value="light"<?php selected( $instance['theme'], 'light' ); ?>>
  410. <?php esc_html_e( 'Light', 'jetpack' ); ?>
  411. </option>
  412. <option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>>
  413. <?php esc_html_e( 'Dark', 'jetpack' ); ?>
  414. </option>
  415. </select>
  416. </p>
  417. <?php
  418. }
  419. }