/** * Try to add the views counter for an entity based on the settings of the views_counter plugin * * @param $entity_guid */ function add_views_counter($entity_guid) { $entity = get_entity($entity_guid); if ($entity) { // Get the entities that the views counter was already added $already_added_entities = unserialize(get_input('views_counter', '')); if (!is_array($already_added_entities)) { $already_added_entities = array(); } // Checking if the views counter was already added for this entity if (!in_array($entity->guid, $already_added_entities)) { // To register that the views_counter was added for this entity $already_added_entities[] = $entity->guid; set_input('views_counter', serialize($already_added_entities)); // Get the added types for add a views counter $added_types = unserialize(elgg_get_plugin_setting('add_views_counter', 'views_counter')); // Get the types setted up by the admin for do not allow add the views counter $removed_types = unserialize(elgg_get_plugin_setting('remove_views_counter', 'views_counter')); // Save the subtype $subtype = $entity->getSubtype(); $subtype = $subtype ? $subtype : $entity->type; // If the entity has a added type then increment the views counter if (in_array($subtype, $added_types)) { return increment_views_counter($entity->guid); } else { if (!in_array($subtype, $removed_types)) { // If the views counter is being added for a subtype that was not setted up by the admin then let's set the plugin setting now if (!in_array($subtype, $added_types)) { $added_types[] = $subtype; elgg_set_plugin_setting('add_views_counter', serialize($added_types), 'views_counter'); system_message(sprintf(elgg_echo('views_counter:new_subtype_added'), $subtype)); } return increment_views_counter($entity->guid); } } } } return false; }
/** * Try to add the views counter for an entity based on the settings of the views_counter plugin * * @param $entity_guid */ function add_views_counter($entity_guid) { static $added_entities; if (!is_array($added_entities)) { $added_entities = array(); } if (in_array($entity_guid, $added_entities)) { return false; // we've already added this view } $entity = get_entity($entity_guid); if (!$entity) { return false; } // save this guid so we know it's been processed in the future $added_entities[] = $entity_guid; // Get the added types for add a views counter $added_types = unserialize(elgg_get_plugin_setting('add_views_counter', PLUGIN_ID)); // Get the types set up by the admin to not allow the views counter $removed_types = unserialize(elgg_get_plugin_setting('remove_views_counter', PLUGIN_ID)); // Save the subtype $subtype = $entity->getSubtype(); $subtype = $subtype ? $subtype : $entity->type; // If the entity has a added type then increment the views counter if (in_array($subtype, $added_types)) { return increment_views_counter($entity->guid); } else { if (!in_array($subtype, $removed_types)) { // If the views counter is being added for a subtype that was not set up by the admin then let's set the plugin setting now if (!in_array($subtype, $added_types)) { $added_types[] = $subtype; elgg_set_plugin_setting('add_views_counter', serialize($added_types), PLUGIN_ID); } return increment_views_counter($entity->guid); } } return false; }