Projet

Général

Profil

Révision 66b5cbf6

Ajouté par Assos Assos il y a presque 10 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/twitter_block/twitter_block.module
5 5
 * A module to provide simple Twitter blocks using the Twitter Search API.
6 6
 */
7 7

  
8
/**
9
 * Implements hook_cron().
10
 */
11
function twitter_block_cron() {
12
  // Regenerate the JavaScript file every day.
13
  if (REQUEST_TIME - variable_get('twitter_block_last_cache', 0) >= 86400 && variable_get('twitter_block_cache', TRUE)) {
14
    // Synchronize the widget code and update it if remote file have changed.
15
    twitter_block_cache(TRUE);
16

  
17
    // Record when the synchronization occurred.
18
    variable_set('twitter_block_last_cache', REQUEST_TIME);
19
  }
20
}
21

  
8 22
/**
9 23
 * Implements hook_help().
10 24
 */
......
19 33
 * Implements hook_menu().
20 34
 */
21 35
function twitter_block_menu() {
22
  // Create an array of block settings
36
  // Create an array of block settings.
23 37
  $settings = array(
24 38
    'title' => 'Add Twitter block',
25 39
    'description' => 'Add a new Twitter block.',
......
29 43
    'file' => 'twitter_block.admin.inc',
30 44
  );
31 45

  
32
  // Add a local action to the block configuration page
46
  // Add a local action to the block configuration page.
33 47
  $items['admin/structure/block/add-twitter-block'] = array(
34 48
    'access arguments' => array('administer blocks'),
35 49
  ) + $settings;
36 50

  
37
  // Get the default site theme
51
  // Get the default site theme.
38 52
  $default_theme = variable_get('theme_default', 'bartik');
39 53

  
40
  // Add a local action to the per-theme block configuration pages
54
  // Add a local action to the per-theme block configuration pages.
41 55
  foreach (list_themes() as $key => $theme) {
42 56
    if ($key != $default_theme) {
43 57
      $items['admin/structure/block/list/' . $key . '/add-twitter-block'] = array(
......
71 85
function twitter_block_form_block_admin_display_form_alter(&$form, &$form_state, $form_id) {
72 86
  $result = db_query('SELECT bid FROM {twitter_block}');
73 87

  
74
  // Add delete links to Twitter Block blocks
88
  // Add delete links to Twitter Block blocks.
75 89
  foreach ($result as $block) {
76 90
    $form['blocks']['twitter_block_' . $block->bid]['delete'] = array(
77 91
      '#type' => 'link',
......
93 107
 *   - bid: Block ID.
94 108
 *   - info: Block description.
95 109
 *   - widget_id: Widget ID.
110
 *   - username: Account username.
96 111
 *   - theme: Theme.
97 112
 *   - link_color: Link color.
98 113
 *   - width: Width.
......
157 172
  $edit += array(
158 173
    'info' => '',
159 174
    'widget_id' => '',
175
    'username' => '',
160 176
    'theme' => '',
161 177
    'link_color' => '',
162 178
    'width' => '',
......
184 200
    '#required' => TRUE,
185 201
    '#description' => t('Each Twitter Block block requires a unique widget ID which determines, among other things, the source (user timeline, favourites, list or search) of the tweets to display. You can view a list of your existing embedded timeline widgets (and their widget IDs) or create new embedded timeline widgets by visiting the <a href="@widgets_section">widgets section of your settings page</a> (make sure that you\'re logged in). You can determine a widget\'s ID by editing it and inspecting the URL (which should be in the form of twitter.com/settings/widgets/WIDGET_ID/edit) or by looking at the widget\'s embed code (look for data-widget-id="WIDGET_ID").', array('@widgets_section' => 'https://twitter.com/settings/widgets')),
186 202
  );
203
  $form['username'] = array(
204
    '#type' => 'textfield',
205
    '#title' => t('Username'),
206
    '#default_value' => $edit['username'],
207
    '#required' => TRUE,
208
    '#description' => t('A Twitter account username. This is used to generate a fallback link to the Twitter account associated with the widget when JavaScript is not available. You can find your account username by visting the <a href="@account_section">account section of your settings page</a> or on your profile page in the URL or prefixed with @ under your display name.', array('@account_section' => 'https://twitter.com/settings/account')),
209
  );
187 210
  $form['appearance'] = array(
188 211
    '#type' => 'fieldset',
189 212
    '#title' => t('Appearance'),
......
311 334
 *   Associative array of fields to save. Array keys:
312 335
 *   - info: Block description.
313 336
 *   - widget_id: Widget ID.
337
 *   - username: Account username.
314 338
 *   - theme: Theme.
315 339
 *   - link_color: Link color.
316 340
 *   - width: Width.
......
342 366
    'polite' => $edit['polite'],
343 367
  );
344 368

  
345
  // Save the block configuration
369
  // Save the block configuration.
346 370
  $delta = db_update('twitter_block')
347 371
    ->fields(array(
348 372
      'info' => $edit['info'],
349 373
      'widget_id' => $edit['widget_id'],
374
      'username' => $edit['username'],
350 375
      'data' => serialize($data),
351 376
    ))
352 377
    ->condition('bid', $delta)
......
362 387
  // Load the configuration.
363 388
  $config = twitter_block_block_get($delta);
364 389

  
365
  // Unserialize the timeline 
390
  // Unserialize the timeline.
366 391
  $data = unserialize($config['data']);
367 392

  
368 393
  $block = array();
369 394
  $block['subject'] = check_plain($config['info']);
370 395
  $block['content'] = array(
371
    '#theme' => 'link',
372
    '#text' => 'Tweets by @twitterapi',
373
    '#path' => 'https://twitter.com/twitterapi',
396
    '#type' => 'link',
397
    '#title' => t('Tweets by @username', array('@username' => $config['username'])),
398
    '#href' => 'https://twitter.com/' . $config['username'],
374 399
    '#options' => array(
375 400
      'attributes' => array(
376 401
        'class' => array('twitter-timeline'),
......
378 403
      ),
379 404
      'html' => FALSE,
380 405
    ),
381
    '#attached' => array(
382
      'js' => array(
383
        'https://platform.twitter.com/widgets.js' => array(
384
          'type' => 'external',
385
        ),
386
      ),
387
    ),
388 406
  );
389 407

  
408
  // Use a locally cached copy of widgets.js by default.
409
  if (variable_get('twitter_block_cache', TRUE) && $url = twitter_block_cache()) {
410
    $block['content']['#attached']['js'] = array(
411
      array(
412
        'data' => $url,
413
        'type' => 'file',
414
      ),
415
    );
416
  }
417
  else {
418
    $block['content']['#attached']['js'] = array(
419
      '//platform.twitter.com/widgets.js' => array(
420
        'type' => 'external',
421
        'scope' => 'footer',
422
      ),
423
    );
424
  }
425

  
390 426
  if (!empty($data['theme'])) {
391 427
    $block['content']['#options']['attributes']['data-theme'] = $data['theme'];
392 428
  }
......
434 470
  }
435 471

  
436 472
  if (!empty($data['polite'])) {
437
    $block['content']['#options']['attributes']['aria-polite'] = $data['polite'];
473
    $block['content']['#options']['attributes']['data-aria-polite'] = $data['polite'];
438 474
  }
439 475

  
440 476
  return $block;
441 477
}
478

  
479
/**
480
 * Download/Synchronize/Cache widget code file locally.
481
 *
482
 * @param $sync_cached_file
483
 *   Synchronize widget code and update if remote file have changed.
484
 * @return mixed
485
 *   The path to the local javascript file on success, boolean FALSE on failure.
486
 */
487
function twitter_block_cache($sync_cached_file = FALSE) {
488
  $location = 'http://platform.twitter.com/widgets.js';
489
  $path = 'public://twitter_block';
490
  $file_destination = $path . '/' . basename($location);
491

  
492
  if (!file_exists($file_destination) || $sync_cached_file) {
493
    // Download the latest widget code.
494
    $result = drupal_http_request($location);
495

  
496
    if ($result->code == 200) {
497
      if (file_exists($file_destination)) {
498
        // Synchronize widget code and and replace local file if outdated.
499
        $data_hash_local = drupal_hash_base64(file_get_contents($file_destination));
500
        $data_hash_remote = drupal_hash_base64($result->data);
501
        // Check that the files directory is writable.
502
        if ($data_hash_local != $data_hash_remote && file_prepare_directory($path)) {
503
          // Save updated widget code file to disk.
504
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
505
          watchdog('twitter_block', 'Locally cached widget code file has been updated.', array(), WATCHDOG_INFO);
506

  
507
          // Change query-strings on css/js files to enforce reload for all users.
508
          _drupal_flush_css_js();
509
        }
510
      }
511
      else {
512
        // Check that the files directory is writable.
513
        if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
514
          // There is no need to flush JS here as core refreshes JS caches
515
          // automatically, if new files are added.
516
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
517
          watchdog('twitter_block', 'Locally cached widget code file has been saved.', array(), WATCHDOG_INFO);
518

  
519
          // Return the local JS file path.
520
          return file_create_url($file_destination);
521
        }
522
      }
523
    }
524
  }
525
  else {
526
    // Return the local JS file path.
527
    return file_create_url($file_destination);
528
  }
529
}
530

  
531
/**
532
 * Delete cached files and directory.
533
 */
534
function twitter_block_clear_js_cache() {
535
  $path = 'public://twitter_block';
536
  if (file_prepare_directory($path)) {
537
    file_scan_directory($path, '/.*/', array('callback' => 'file_unmanaged_delete'));
538
    drupal_rmdir($path);
539

  
540
    // Change query-strings on css/js files to enforce reload for all users.
541
    _drupal_flush_css_js();
542

  
543
    watchdog('twitter_block', 'Local cache has been purged.', array(), WATCHDOG_INFO);
544
  }
545
}

Formats disponibles : Unified diff