diff --git a/fivestar.migrate.inc b/fivestar.migrate.inc index 77a6d90..9e468b4 100644 --- a/fivestar.migrate.inc +++ b/fivestar.migrate.inc @@ -22,14 +22,14 @@ function fivestar_migrate_api() { class MigrateFivestarFieldHandler extends MigrateFieldHandler { /** - * Constructor function. + * Constructor, initialize registerTypes. */ public function __construct() { $this->registerTypes(array('fivestar')); } /** - * Function fields() implementation. + * {@inheritdoc} */ public function fields($type, $parent_field, $migration = NULL) { $fields = array( @@ -54,7 +54,7 @@ class MigrateFivestarFieldHandler extends MigrateFieldHandler { $return[$language][$delta] = array('rating' => $value) + array_intersect_key($arguments, $field_info['columns']); } - return isset($return) ? $return : NULL; + return $return ?? NULL; } } diff --git a/fivestar.module b/fivestar.module index 83a9316..aa49890 100644 --- a/fivestar.module +++ b/fivestar.module @@ -201,13 +201,12 @@ function _fivestar_variables() { } /** - * Internal function to handle vote casting, flood control, XSS, IP based - * voting, etc... + * Internal function to handle vote casting, flood control, XSS, IP based etc.. */ function _fivestar_cast_vote($entity_type, $id, $value, $tag = NULL, $uid = NULL, $skip_validation = FALSE, $vote_source = NULL) { global $user; $tag = empty($tag) ? 'vote' : $tag; - $uid = isset($uid) ? $uid : $user->uid; + $uid = $uid ?? $user->uid; // Bail out if the user's trying to vote on an invalid object. if (!$skip_validation && !fivestar_validate_target($entity_type, $id, $tag, $uid)) { return array(); @@ -260,14 +259,14 @@ function _fivestar_cast_vote($entity_type, $id, $value, $tag = NULL, $uid = NULL * * @param string $entity_type * The Entity type for which to retrieve votes. - * @param int $id + * @param int $ids * The ID for which to retrieve votes. * @param string $tag * The VotingAPI tag for which to retrieve votes. * @param int $uid * Optional. A user ID for which to retrieve votes. * - * @return + * @return array * An array of the following keys: * - average: An array of VotingAPI results, including the average 'value'. * - count: An array of VotingAPI results, including the count 'value'. @@ -332,6 +331,20 @@ function fivestar_get_votes_multiple($entity_type, $ids, $tag = 'vote', $uid = N /** * Implementing function fivestar_get_votes(). + * + * @param string $entity_type + * The Entity type for which to retrieve votes. + * @param int $id + * The ID for which to retrieve votes. + * @param string $tag + * The VotingAPI tag for which to retrieve votes. + * @param int $uid + * Optional. A user ID for which to retrieve votes. + * + * @return array + * An array of the following keys: + * - entity_type: An array of VotingAPI results, including the entity_type. + * - id: An array of VotingAPI results, including the id. */ function fivestar_get_votes($entity_type, $id, $tag = 'vote', $uid = NULL) { $multiple = fivestar_get_votes_multiple($entity_type, array($id), $tag, $uid); @@ -351,6 +364,8 @@ function fivestar_get_votes($entity_type, $id, $tag = 'vote', $uid = NULL) { * The user trying to cast the vote. * * @return bool + * Returns TRUE if vote is a valid. + * Returns FALSE if vote is invalid. */ function fivestar_validate_target($entity_type, $id, $tag, $uid = NULL) { if (!isset($uid)) { @@ -497,7 +512,7 @@ function fivestar_fivestar_widgets() { * - tag: The VotingAPI tag that will be registered by this widget. Defaults * to "vote". */ -function fivestar_custom_widget($form, &$form_state, $values, $settings) { +function fivestar_custom_widget($form, array $form_state, array $values, array $settings) { $form = array( '#attributes' => array( 'class' => array('fivestar-widget'), @@ -579,9 +594,9 @@ function fivestar_ajax_submit($form, $form_state) { } $values = array(); - $values['user'] = isset($votes['user']['value']) ? $votes['user']['value'] : 0; - $values['average'] = isset($votes['average']['value']) ? $votes['average']['value'] : 0; - $values['count'] = isset($votes['count']['value']) ? $votes['count']['value'] : 0; + $values['user'] = $votes['user']['value'] ?? 0; + $values['average'] = $votes['average']['value'] ?? 0; + $values['count'] = $votes['count']['value'] ?? 0; // We need to process the 'fivestar' element with the new values. $form['vote']['#values'] = $values; @@ -590,7 +605,7 @@ function fivestar_ajax_submit($form, $form_state) { $new_element = fivestar_expand($form['vote'], $form_state, $form_state['complete form']); // Update the description. Since it doesn't account of our cast vote above. // @todo Look into all this, I honestly don't like it and it's a bit weird. - $form['vote']['vote']['#description'] = isset($new_element['vote']['#description']) ? $new_element['vote']['#description'] : ''; + $form['vote']['vote']['#description'] = $new_element['vote']['#description'] ?? ''; if (!$form['vote']['#allow_revote'] && !$form['vote']['#allow_clear']) { $form['vote'] = $new_element; } @@ -765,8 +780,8 @@ function fivestar_expand($element, $form_state, $complete_form) { )); if ($settings['text'] != 'none') { $static_description = theme('fivestar_summary', array( - 'average_rating' => $settings['text'] == 'user' ? NULL : (isset($values['average']) ? $values['average'] : 0), - 'votes' => isset($values['count']) ? $values['count'] : 0, + 'average_rating' => $settings['text'] == 'user' ? NULL : ($values['average'] ?? 0), + 'votes' => $values['count'] ?? 0, 'stars' => $settings['stars'], )); } @@ -830,10 +845,10 @@ function fivestar_expand($element, $form_state, $complete_form) { * @param array $element * The fivestar element. * - * @return + * @return default_value * The default value for the element. */ -function _fivestar_get_element_default_value($element) { +function _fivestar_get_element_default_value(array $element) { if (isset($element['#default_value'])) { $default_value = $element['#default_value']; } @@ -889,7 +904,7 @@ function fivestar_votingapi_metadata_alter(&$data) { foreach (fivestar_get_tags() as $tag) { // Add several custom tags that are being used by fivestar. $data['tags'][$tag] = array( - 'name' => t($tag), + 'name' => $tag, 'description' => t('@tag used by fivestar.', array('@tag' => $tag)), 'module' => 'fivestar', ); @@ -1023,6 +1038,7 @@ function fivestar_fivestar_target_info($field, $instance) { } /** + * Implementing function _fivestar_target_node_reference(). * * @return array * array('entity_type', 'entity_id'). @@ -1040,7 +1056,10 @@ function _fivestar_target_node_reference($entity, $field, $instance, $langcode) } /** - * @return (array) array('entity_type', 'entity_id') + * Implementing function _fivestar_target_entityreference(). + * + * @return array + * array('entity_type', 'entity_id'). */ function _fivestar_target_entityreference($entity, $field, $instance, $langcode) { $target = array(); @@ -1059,6 +1078,7 @@ function _fivestar_target_entityreference($entity, $field, $instance, $langcode) } /** + * Implementing function _fivestar_target_user_reference(). * * @return array * array('entity_type', 'entity_id'). @@ -1089,6 +1109,8 @@ function _fivestar_target_comment_parent_node($entity, $field, $instance, $langc * Helper function to determine if a user can vote on content. * * @return bool + * Returns TRUE if a user can vote. + * Returns FALSE if a user cannot vote. */ function _fivestar_allow_vote($element) { global $user; @@ -1100,9 +1122,9 @@ function _fivestar_allow_vote($element) { } else { $criteria = array( - 'entity_id' => isset($element['#settings']['content_id']) ? $element['#settings']['content_id'] : NULL, - 'entity_type' => isset($element['#settings']['content_type']) ? $element['#settings']['content_type'] : NULL, - 'tag' => isset($element['#settings']['tag']) ? $element['#settings']['tag'] : NULL, + 'entity_id' => $element['#settings']['content_id'] ?? NULL, + 'entity_type' => $element['#settings']['content_type'] ?? NULL, + 'tag' => $element['#settings']['tag'] ?? NULL, 'uid' => $user->uid, ); @@ -1128,11 +1150,11 @@ function _fivestar_allow_vote($element) { } /** - * Implements hook_ctools_content_subtype_alter() + * Implements hook_alter(). */ function fivestar_ctools_content_subtype_alter(&$subtype, &$plugin) { if ($plugin['name'] == 'entity_field' && isset($subtype['subtype_id'])) { - list($entity_type, $field_name) = explode(':', $subtype['subtype_id'], 2); + [$entity_type, $field_name] = explode(':', $subtype['subtype_id'], 2); $field = field_info_field($field_name); if ($field['type'] == 'fivestar') { $subtype['render callback'] = 'fivestar_fivestar_field_content_type_render'; @@ -1150,7 +1172,7 @@ function fivestar_fivestar_field_content_type_render($subtype, $conf, $panel_arg // Get a shortcut to the entity. $entity = $context->data; - list($entity_type, $field_name) = explode(':', $subtype, 2); + [$entity_type, $field_name] = explode(':', $subtype, 2); // Load the entity type's information for this field. $ids = entity_extract_ids($entity_type, $entity); diff --git a/includes/fivestar.field.inc b/includes/fivestar.field.inc index 0407aab..3d1b045 100644 --- a/includes/fivestar.field.inc +++ b/includes/fivestar.field.inc @@ -55,7 +55,7 @@ function fivestar_field_settings_form($field, $instance) { '#title' => 'Voting Tag', '#options' => fivestar_get_tags(), '#description' => t('The tag this rating will affect. Enter a property on which that this rating will affect, such as quality, satisfaction, overall, etc.'), - '#default_value' => isset($field['settings']['axis']) ? $field['settings']['axis'] : '', + '#default_value' => $field['settings']['axis'] ?? '', '#disabled' => field_has_data($field), ); @@ -73,27 +73,27 @@ function fivestar_field_instance_settings_form($field, $instance) { '#type' => 'select', '#title' => check_plain($widget_title), '#options' => drupal_map_assoc(range(1, 10)), - '#default_value' => isset($instance['settings']['stars']) ? $instance['settings']['stars'] : 5, + '#default_value' => $instance['settings']['stars'] ?? 5, ); $form['allow_clear'] = array( '#type' => 'checkbox', '#title' => t('Allow users to cancel their ratings.'), - '#default_value' => isset($instance['settings']['allow_clear']) ? $instance['settings']['allow_clear'] : FALSE, + '#default_value' => $instance['settings']['allow_clear'] ?? FALSE, '#return_value' => 1, ); $form['allow_revote'] = array( '#type' => 'checkbox', '#title' => t('Allow users to re-vote on already voted content.'), - '#default_value' => isset($instance['settings']['allow_revote']) ? $instance['settings']['allow_revote'] : TRUE, + '#default_value' => $instance['settings']['allow_revote'] ?? TRUE, '#return_value' => 1, ); $form['allow_ownvote'] = array( '#type' => 'checkbox', '#title' => t('Allow users to vote on their own content.'), - '#default_value' => isset($instance['settings']['allow_ownvote']) ? $instance['settings']['allow_ownvote'] : TRUE, + '#default_value' => $instance['settings']['allow_ownvote'] ?? TRUE, '#return_value' => 1, ); @@ -194,7 +194,7 @@ function _fivestar_field_target($entity, $field, $instance, $item, $langcode) { else { // If all else fails, default to voting on the // instance the field is attached to. - list($id, $vid, $bundle) = entity_extract_ids($instance['entity_type'], $entity); + [$id, $vid, $bundle] = entity_extract_ids($instance['entity_type'], $entity); $target = array( 'entity_id' => $id, 'entity_type' => $instance['entity_type'], @@ -208,7 +208,7 @@ function _fivestar_field_target($entity, $field, $instance, $item, $langcode) { */ function _fivestar_update_field_value($entity_type, $entity, $field_name, $langcode, $value) { $entity->{$field_name}[$langcode][0]['rating'] = $value; - $entity->original = isset($entity->original) ? $entity->original : NULL; + $entity->original = $entity->original ?? NULL; field_attach_presave($entity_type, $entity); field_attach_update($entity_type, $entity); } @@ -269,7 +269,7 @@ function fivestar_field_widget_settings_form($field, $instance) { $form['widget']['fivestar_widget'] = array( '#type' => 'radios', '#options' => array('default' => t('Default')) + $widgets, - '#default_value' => isset($instance['widget']['settings']['widget']['fivestar_widget']) ? $instance['widget']['settings']['widget']['fivestar_widget'] : 'default', + '#default_value' => $instance['widget']['settings']['widget']['fivestar_widget'] ?? 'default', '#attributes' => array('class' => array('fivestar-widgets', 'clearfix')), '#pre_render' => array('fivestar_previews_expand'), '#attached' => array('css' => array(drupal_get_path('module', 'fivestar') . '/css/fivestar-admin.css')), @@ -314,15 +314,15 @@ function fivestar_field_widget_form(&$form, &$form_state, $field, $instance, $la '#type' => 'select', '#title' => check_plain(isset($instance['label']) ? (($i18n) ? i18n_field_translate_property($instance, 'label') : t($instance['label'])) : FALSE), '#options' => $options, - '#default_value' => isset($items[$delta]['rating']) ? $items[$delta]['rating'] : NULL, + '#default_value' => $items[$delta]['rating'] ?? NULL, '#description' => check_plain(isset($instance['description']) ? (($i18n) ? i18n_field_translate_property($instance, 'description') : t($instance['description'])) : FALSE), - '#required' => isset($instance['required']) ? $instance['required'] : FALSE, + '#required' => $instance['required'] ?? FALSE, ); } elseif ($instance['widget']['type'] == 'stars') { $widgets = module_invoke_all('fivestar_widgets'); - $active = isset($instance['widget']['settings']['widget']['fivestar_widget']) ? $instance['widget']['settings']['widget']['fivestar_widget'] : 'default'; + $active = $instance['widget']['settings']['widget']['fivestar_widget'] ?? 'default'; $widget = array( 'name' => isset($widgets[$active]) ? strtolower($widgets[$active]) : 'default', 'css' => $active, @@ -347,16 +347,16 @@ function fivestar_field_widget_form(&$form, &$form_state, $field, $instance, $la $element['rating'] = array( '#type' => 'fivestar', '#title' => check_plain(isset($instance['label']) ? (($i18n) ? i18n_field_translate_property($instance, 'label') : t($instance['label'])) : FALSE), - '#stars' => isset($instance['settings']['stars']) ? $instance['settings']['stars'] : 5, - '#allow_clear' => isset($instance['settings']['allow_clear']) ? $instance['settings']['allow_clear'] : FALSE, - '#allow_revote' => isset($instance['settings']['allow_revote']) ? $instance['settings']['allow_revote'] : FALSE, - '#allow_ownvote' => isset($instance['settings']['allow_ownvote']) ? $instance['settings']['allow_ownvote'] : FALSE, - '#default_value' => isset($items[$delta]['rating']) ? $items[$delta]['rating'] : (isset($instance['default_value'][$delta]['rating']) ? $instance['default_value'][$delta]['rating'] : 0), + '#stars' => $instance['settings']['stars'] ?? 5, + '#allow_clear' => $instance['settings']['allow_clear'] ?? FALSE, + '#allow_revote' => $instance['settings']['allow_revote'] ?? FALSE, + '#allow_ownvote' => $instance['settings']['allow_ownvote'] ?? FALSE, + '#default_value' => $items[$delta]['rating'] ?? ($instance['default_value'][$delta]['rating'] ?? 0), '#widget' => $widget, '#settings' => $settings, '#values' => $values, '#description' => check_plain(isset($instance['description']) ? (($i18n) ? i18n_field_translate_property($instance, 'description') : t($instance['description'])) : FALSE), - '#required' => isset($instance['required']) ? $instance['required'] : FALSE, + '#required' => $instance['required'] ?? FALSE, ); } @@ -420,7 +420,7 @@ function fivestar_field_formatter_settings_form($field, $instance, $view_mode, $ $element['widget']['fivestar_widget'] = array( '#type' => 'radios', '#options' => array('default' => t('Default')) + $widgets, - '#default_value' => isset($settings['widget']['fivestar_widget']) ? $settings['widget']['fivestar_widget'] : 'default', + '#default_value' => $settings['widget']['fivestar_widget'] ?? 'default', '#attributes' => array('class' => array('fivestar-widgets', 'clearfix')), '#pre_render' => array('fivestar_previews_expand'), '#attached' => array('css' => array(drupal_get_path('module', 'fivestar') . '/css/fivestar-admin.css')), @@ -486,7 +486,7 @@ function fivestar_field_formatter_settings_summary($field, $instance, $view_mode } $summary = t("Style: @widget, Stars display: @style, Text display: @text", array( - '@widget' => isset($widgets[$settings['widget']['fivestar_widget']]) ? $widgets[$settings['widget']['fivestar_widget']] : t('default'), + '@widget' => $widgets[$settings['widget']['fivestar_widget']] ?? t('default'), '@style' => strtolower($settings['style']), '@text' => strtolower($settings['text']), )); @@ -533,7 +533,7 @@ function fivestar_field_formatter_view($entity_type, $entity, $field, $instance, // @todo Get rid of voting categories setting, then change this so // axis = field name. $tag = (isset($field['settings']['axis'])) ? $field['settings']['axis'] : 'vote'; - list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); + [$id, $vid, $bundle] = entity_extract_ids($entity_type, $entity); $settings = _fivestar_custom_widget_settings($entity_type, $instance, $display, $id, $tag, $widget); // Store entity and field data for later reuse. $settings += array( @@ -776,9 +776,9 @@ function fivestar_field_prepare_view($entity_type, $entities, $field, $instances foreach ($votes[$entity_type] as $id => $vote) { // Populating the $items[$id] array even for items with no value forces // the render system to output a widget. - $values['user'] = isset($vote['user']['value']) ? $vote['user']['value'] : 0; - $values['average'] = isset($vote['average']['value']) ? $vote['average']['value'] : 0; - $values['count'] = isset($vote['count']['value']) ? $vote['count']['value'] : 0; + $values['user'] = $vote['user']['value'] ?? 0; + $values['average'] = $vote['average']['value'] ?? 0; + $values['count'] = $vote['count']['value'] ?? 0; $items[$id] = array($values); } } @@ -828,7 +828,7 @@ function _fivestar_field_property_values($entity, array $options, $name, $entity $values = array(); if (isset($entity->{$name}[$langcode])) { foreach ($entity->{$name}[$langcode] as $delta => $data) { - $values[$delta]['user_rating'] = isset($data['rating']) ? $data['rating'] : NULL; + $values[$delta]['user_rating'] = $data['rating'] ?? NULL; $result = array(); $result = votingapi_select_results(array( 'entity_type' => $entity_type, @@ -837,7 +837,7 @@ function _fivestar_field_property_values($entity, array $options, $name, $entity 'tag' => $field['settings']['axis'], 'function' => 'average', )); - $values[$delta]['average_rating'] = (isset($result[0]['value']) ? $result[0]['value'] : 0); + $values[$delta]['average_rating'] = ($result[0]['value'] ?? 0); } } // For an empty single-valued field, we have to return NULL. diff --git a/includes/fivestar.theme.inc b/includes/fivestar.theme.inc index 2983647..9c083db 100644 --- a/includes/fivestar.theme.inc +++ b/includes/fivestar.theme.inc @@ -33,7 +33,7 @@ function theme_fivestar_preview_widget($variables) { '#allow_revote' => TRUE, '#allow_ownvote' => TRUE, '#widget' => array( - 'name' => isset($variables['name']) ? $variables['name'] : 'default', + 'name' => $variables['name'] ?? 'default', 'css' => isset($variables['css']) && $variables['css'] != 'default' ? $variables['css'] : FALSE, ), );