/** * Check if resharing of this entity is allowed * * @param \ElggEntity $entity the entity to check * * @return bool */ protected static function canReshareEntity(\ElggEntity $entity) { if (!$entity instanceof \ElggEntity) { return false; } // only allow objects and groups if (!$entity instanceof \ElggObject && !$entity instanceof \ElggGroup) { return false; } // comments and discussion replies are never allowed $blocked_subtypes = ['comment', 'discussion_reply']; if (in_array($entity->getSubtype(), $blocked_subtypes)) { return false; } // by default allow searchable entities $reshare_allowed = false; if ($entity instanceof \ElggGroup) { $reshare_allowed = true; } else { $searchable_entities = get_registered_entity_types($entity->getType()); if (!empty($searchable_entities)) { $reshare_allowed = in_array($entity->getSubtype(), $searchable_entities); } } // trigger hook to allow others to change $params = ['entity' => $entity, 'user' => elgg_get_logged_in_user_entity()]; return (bool) elgg_trigger_plugin_hook('reshare', $entity->getType(), $params, $reshare_allowed); }
/** * Check if entity type is registered for feature/unfeature * * @param ElggEntity $entity Entity to be featured * @return bool */ function actions_feature_is_allowed_type(ElggEntity $entity) { $type = $entity->getType(); $subtype = $entity->getSubtype(); $hook_type = implode(':', array_filter([$type, $subtype])); return elgg_trigger_plugin_hook('feature', $hook_type, ['entity' => $entity], false); }
/** * Checks if the given $entity is registered for notifications by * register_notification_object() * * @param ElggEntity $entity the entity to check * @param bool $subject return the subject string (default=false) * * @return bool|string */ function advanced_notifications_is_registered_notification_entity(ElggEntity $entity, $subject = false) { $result = false; if (!empty($entity) && $entity instanceof ElggEntity) { $type = $entity->getType(); if (empty($type)) { $type = "__BLANK__"; } $subtype = $entity->getSubtype(); if (empty($subtype)) { $subtype = "__BLANK__"; } // get the registered entity -> type/subtype $notifications = elgg_get_config("register_objects"); if (!empty($notifications) && is_array($notifications)) { if (isset($notifications[$type]) && isset($notifications[$type][$subtype])) { if ($subject) { $result = $notifications[$type][$subtype]; } else { $result = true; } } } } return $result; }
public function testSimpleGetters() { $this->obj->type = 'foo'; $this->obj->subtype = 'subtype'; $this->obj->owner_guid = 77; $this->obj->access_id = 2; $this->obj->time_created = 123456789; $this->assertEquals($this->obj->getGUID(), $this->obj->guid); $this->assertEquals($this->obj->getType(), $this->obj->type); // Note: before save() subtype returns string, int after // see https://github.com/Elgg/Elgg/issues/5920#issuecomment-25246298 $this->assertEquals($this->obj->getSubtype(), $this->obj->subtype); $this->assertEquals($this->obj->getOwnerGUID(), $this->obj->owner_guid); $this->assertEquals($this->obj->getAccessID(), $this->obj->access_id); $this->assertEquals($this->obj->getTimeCreated(), $this->obj->time_created); $this->assertEquals($this->obj->getTimeUpdated(), $this->obj->time_updated); }
/** * Track certain events on ElggEntity's * * @param string $event the event * @param string $type the type of the ElggEntity * @param \ElggEntity $object the entity for the event * * @return void */ public static function events($event, $type, $object) { if (!$object instanceof \ElggEntity) { return; } if (!analytics_google_track_events_enabled()) { return; } switch ($object->getType()) { case 'object': analytics_track_event($object->getSubtype(), $event, $object->title); break; case 'group': case 'user': analytics_track_event($object->getType(), $event, $object->name); break; } }
/** * Count how many people have liked an entity. * * @param ElggEntity $entity * * @return int Number of likes */ function likes_count($entity) { $type = $entity->getType(); $params = array('entity' => $entity); $number = elgg_trigger_plugin_hook('likes:count', $type, $params, false); if ($number) { return $number; } else { return $entity->countAnnotations('likes'); } }
/** * Set priority of an element in a list * * @see ElggEntity::$priority * * @param ElggEntity $entity * @return bool */ function hj_framework_set_entity_priority($entity, $priority = null) { if ($priority) { $entity->priority = $priority; return true; } $count = elgg_get_entities(array('type' => $entity->getType(), 'subtype' => $entity->getSubtype(), 'owner_guid' => $entity->owner_guid, 'container_guid' => $entity->container_guid, 'count' => true)); if (!$entity->priority) { $entity->priority = $count + 1; } return true; }
/** * Prepares object type string * @return string */ public function getObjectType() { $type = $this->object->getType(); $subtype = $this->object->getSubtype() ?: 'default'; $keys = ["interactions:{$type}:{$subtype}", $this->object instanceof Comment ? "interactions:comment" : "interactions:post"]; foreach ($keys as $key) { if (elgg_language_key_exists($key, $this->language)) { return elgg_echo($key, array(), $this->language); } } return elgg_echo('interactions:post', $this->language); }
/** * @requires PHP 5.3.2 */ public function testSimpleGetters() { $this->obj->type = 'foo'; $this->obj->subtype = 'subtype'; $this->obj->owner_guid = 77; $this->obj->access_id = 2; $this->obj->time_created = 123456789; $this->assertEquals($this->obj->getGUID(), $this->obj->guid); $this->assertEquals($this->obj->getType(), $this->obj->type); $this->assertEquals($this->obj->getSubtype(), $this->obj->subtype); $this->assertEquals($this->obj->getOwnerGUID(), $this->obj->owner_guid); $this->assertEquals($this->obj->getAccessID(), $this->obj->access_id); $this->assertEquals($this->obj->getTimeCreated(), $this->obj->time_created); $this->assertEquals($this->obj->getTimeUpdated(), $this->obj->time_updated); }
/** * {@inheritdoc} */ public function getValues(\ElggEntity $entity) { $sticky = $this->getStickyValue(); if ($sticky) { return $sticky; } $name = $this->getShortname(); switch ($name) { default: return $entity->{$name} ?: $this->getDefaultValue(); case 'type': return $entity->getType(); case 'subtype': return $entity->getSubtype(); } }
function evan_user_can($verb, Entity $object, Entity $target = NULL) { switch ($verb) { case 'post': if (!$target) { $target = elgg_get_logged_in_user_entity(); } $result = $target->canWriteToContainer(0, $object->getType(), $object->getSubtype()); break; case 'update': $result = $object->canEdit(); break; default: $result = false; break; } return elgg_trigger_plugin_hook("permission", $verb, array('actor' => elgg_get_logged_in_user_entity(), 'verb' => $verb, 'object' => $object, 'target' => $target), $result); }
/** * Returns attribute names for an entity * * @param ElggEntity $entity Entity * @return array */ public function getAttributeNames($entity) { if (!$entity instanceof \ElggEntity) { return array(); } $default = array('guid', 'type', 'subtype', 'owner_guid', 'container_guid', 'site_guid', 'access_id', 'time_created', 'time_updated', 'last_action', 'enabled'); switch ($entity->getType()) { case 'user': $attributes = array('name', 'username', 'email', 'language', 'banned', 'admin', 'password', 'salt'); break; case 'group': $attributes = array('name', 'description'); break; case 'object': $attributes = array('title', 'description'); break; } return array_merge($default, $attributes); }
/** * Checks if media type is allowed for given entity * * @param \ElggEntity $entity Entity * @param string $type Media type * @return bool */ function elgg_media_get_thumb_sizes(\ElggEntity $entity, $type = 'icon') { $hook_type = implode(':', array_filter([$entity->getType(), $entity->getSubtype()])); $hook_params = ['type' => $type, 'entity' => $entity]; return elgg_trigger_plugin_hook("entity:{$type}:sizes", $hook_type, $hook_params, []); }
function entity_view_counter_is_counted(ElggEntity $entity) { if (php_sapi_name() === 'cli') { return true; } if (!entity_view_counter_is_configured_entity_type($entity->getType(), $entity->getSubtype())) { return true; } if (isset($_SERVER["HTTP_USER_AGENT"]) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER["HTTP_USER_AGENT"])) { return true; } $user = elgg_get_logged_in_user_entity(); if ($user && $user->getGUID() == $entity->getOwnerGUID()) { return true; } if (is_memcache_available()) { $cache = new ElggMemcache('entity_view_counter'); $key = "view_" . session_id() . "_" . $entity->guid; if ($cache->load($key)) { return true; } } if (entity_view_counter_ignore_ip()) { return true; } return false; }
public function getDocumentTypeFromEntity(\ElggEntity $entity) { $type = $entity->getType(); $subtype = $entity->getSubType(); if (empty($subtype)) { return "{$type}"; } return "{$type}.{$subtype}"; }
/** * View an item in a list * * @param ElggEntity|ElggAnnotation $item * @param array $vars Additional parameters for the rendering * * @return string * @since 1.8.0 * @access private */ function elgg_view_list_item($item, array $vars = array()) { global $CONFIG; $type = $item->getType(); if (in_array($type, $CONFIG->entity_types)) { return elgg_view_entity($item, $vars); } else { if ($type == 'annotation') { return elgg_view_annotation($item, $vars); } else { if ($type == 'river') { return elgg_view_river_item($item, $vars); } } } return ''; }
/** * Prepare entity SEF data * * @param \ElggEntity $entity Entity * @return array|false */ function seo_prepare_entity_data(\ElggEntity $entity) { $path = seo_get_path($entity->getURL()); if (!$path || $path == '/') { return false; } $type = $entity->getType(); switch ($type) { case 'user': if (elgg_is_active_plugin('profile')) { $sef_path = "/profile/{$entity->username}"; } break; case 'group': $subtype = $entity->getSubtype(); $registered = (array) get_registered_entity_types('group'); if (!$subtype || in_array($subtype, $registered)) { if ($subtype && elgg_language_key_exists("item:group:{$subtype}", 'en')) { $prefix = elgg_get_friendly_title(elgg_echo("item:group:{$subtype}", array(), 'en')); } else { $prefix = elgg_get_friendly_title(elgg_echo('item:group', array(), 'en')); } $friendly_title = elgg_get_friendly_title($entity->getDisplayName() ?: ''); $sef_path = "/{$prefix}/{$entity->guid}-{$friendly_title}"; } break; case 'object': $subtype = $entity->getSubtype(); $registered = (array) get_registered_entity_types('object'); if (in_array($subtype, $registered)) { if (elgg_language_key_exists("item:object:{$subtype}", 'en')) { $prefix = elgg_get_friendly_title(elgg_echo("item:object:{$subtype}", array(), 'en')); } else { $prefix = elgg_get_friendly_title($subtype); } $friendly_title = elgg_get_friendly_title($entity->getDisplayName() ?: ''); $sef_path = "/{$prefix}/{$entity->guid}-{$friendly_title}"; } break; } if (!$sef_path) { $sef_path = $path; } $sef_data = seo_get_data($entity->getURL()); if (!is_array($sef_data)) { $sef_data = array(); } $entity_sef_data = ['path' => $path, 'sef_path' => $sef_path, 'title' => $entity->getDisplayName(), 'description' => elgg_get_excerpt($entity->description), 'keywords' => is_array($entity->tags) ? implode(',', $entity->tags) : $entity->tags, 'guid' => $entity->guid]; if ($entity->guid != $entity->owner_guid) { $owner = $entity->getOwnerEntity(); if ($owner) { $entity_sef_data['owner'] = seo_prepare_entity_data($owner); } } if ($entity->guid != $entity->container_guid && $entity->owner_guid != $entity->container_guid) { $container = $entity->getContainerEntity(); if ($container) { $entity_sef_data['container'] = seo_prepare_entity_data($container); } } if (empty($sef_data['admin_defined'])) { $sef_data = array_merge($sef_data, $entity_sef_data); } else { foreach ($entity_sef_data as $key => $value) { if (empty($sef_data[$key])) { $sef_data[$key] = $value; } } } $entity_sef_metatags = elgg_trigger_plugin_hook('metatags', 'discovery', ['entity' => $entity, 'url' => elgg_normalize_url($sef_path)], []); if (!empty($entity_sef_metatags)) { foreach ($entity_sef_metatags as $key => $value) { if (empty($sef_data['admin_defined']) || empty($sef_data['metatags'][$key])) { $sef_data['metatags'][$key] = $value; } } } return $sef_data; }
/** * Get icon size config * * @param \ElggEntity $entity Entity * @param array $icon_sizes Predefined icon sizes * @return array */ public function getSizes(\ElggEntity $entity, array $icon_sizes = array()) { $defaults = $entity && $entity->getSubtype() == 'file' ? $this->config->getFileIconSizes() : $this->config->getGlobalIconSizes(); $sizes = array_merge($defaults, $icon_sizes); return elgg_trigger_plugin_hook('entity:icon:sizes', $entity->getType(), array('entity' => $entity, 'subtype' => $entity->getSubtype()), $sizes); }
/** * When an entity is updated, resets the access ID on all of its child metadata * * @param string $event The name of the event * @param string $object_type The type of object * @param ElggEntity $object The entity itself */ function metadata_update($event, $object_type, $object) { if ($object instanceof ElggEntity) { if (!is_metadata_independent($object->getType(), $object->getSubtype())) { global $CONFIG; $access_id = (int) $object->access_id; $guid = (int) $object->getGUID(); update_data("update {$CONFIG->dbprefix}metadata set access_id = {$access_id} where entity_guid = {$guid}"); } } return true; }
/** * Returns rendered comments and a comment form for an entity. * * @tip Plugins can override the output by registering a handler * for the comments, $entity_type hook. The handler is responsible * for formatting the comments and the add comment form. * * @param ElggEntity $entity The entity to view comments of * @param bool $add_comment Include a form to add comments? * @param array $vars Variables to pass to comment view * * @return string|false Rendered comments or false on failure * @link http://docs.elgg.org/Entities/Comments * @link http://docs.elgg.org/Annotations/Comments */ function elgg_view_comments($entity, $add_comment = true, array $vars = array()) { if (!$entity instanceof ElggEntity) { return false; } $vars['entity'] = $entity; $vars['show_add_form'] = $add_comment; $vars['class'] = elgg_extract('class', $vars, "{$entity->getSubtype()}-comments"); $output = elgg_trigger_plugin_hook('comments', $entity->getType(), $vars, false); if ($output) { return $output; } else { return elgg_view('page/elements/comments', $vars); } }
/** * Checks if a user can subscribe to a content item * * @param ElggEntity $entity the entity to check * @param int $user_guid the user to check (default: current user) * * @return bool */ function content_subscriptions_can_subscribe(ElggEntity $entity, $user_guid = 0) { $user_guid = sanitise_int($user_guid, false); if (empty($user_guid)) { $user_guid = elgg_get_logged_in_user_guid(); } if (empty($user_guid) || !$entity instanceof ElggEntity) { return false; } if ($entity->getOwnerGUID() === $user_guid) { // owner cant subscribe to own content return false; } $supported_entity_types = content_subscriptions_get_supported_entity_types(); if (empty($supported_entity_types)) { return false; } $type = $entity->getType(); if (!isset($supported_entity_types[$type])) { return false; } $subtype = $entity->getSubtype(); if (!empty($subtype)) { return in_array($subtype, $supported_entity_types[$type]); } return true; }
/** * Creates default widgets * * This plugin hook handler is registered for events based on what kinds of * default widgets have been registered. See elgg_default_widgets_init() for * information on registering new default widget contexts. * * @param string $event The event * @param string $type The type of object * @param \ElggEntity $entity The entity being created * @return void * @access private */ function _elgg_create_default_widgets($event, $type, $entity) { $default_widget_info = elgg_get_config('default_widget_info'); if (!$default_widget_info || !$entity) { return; } $type = $entity->getType(); $subtype = $entity->getSubtype(); // event is already guaranteed by the hook registration. // need to check subtype and type. foreach ($default_widget_info as $info) { if ($info['entity_type'] == $type) { if ($info['entity_subtype'] == ELGG_ENTITIES_ANY_VALUE || $info['entity_subtype'] == $subtype) { // need to be able to access everything $old_ia = elgg_set_ignore_access(true); elgg_push_context('create_default_widgets'); // pull in by widget context with widget owners as the site // not using elgg_get_widgets() because it sorts by columns and we don't care right now. $options = array('type' => 'object', 'subtype' => 'widget', 'owner_guid' => elgg_get_site_entity()->guid, 'private_setting_name' => 'context', 'private_setting_value' => $info['widget_context'], 'limit' => 0); $widgets = elgg_get_entities_from_private_settings($options); /* @var \ElggWidget[] $widgets */ foreach ($widgets as $widget) { // change the container and owner $new_widget = clone $widget; $new_widget->container_guid = $entity->guid; $new_widget->owner_guid = $entity->guid; // pull in settings $settings = get_all_private_settings($widget->guid); foreach ($settings as $name => $value) { $new_widget->{$name} = $value; } $new_widget->save(); } elgg_set_ignore_access($old_ia); elgg_pop_context(); } } } }
/** * When an entity is updated, resets the access ID on all of its child metadata * * @param string $event The name of the event * @param string $object_type The type of object * @param \ElggEntity $object The entity itself * * @return true * @access private Set as private in 1.9.0 */ function handleUpdate($event, $object_type, $object) { if ($object instanceof \ElggEntity) { if (!$this->isMetadataIndependent($object->getType(), $object->getSubtype())) { $access_id = (int) $object->access_id; $guid = (int) $object->getGUID(); $query = "update {$this->table} set access_id = {$access_id} where entity_guid = {$guid}"; $this->db->updateData($query); } } return true; }
/** * When an entity is updated, resets the access ID on all of its child metadata * * @param string $event The name of the event * @param string $object_type The type of object * @param ElggEntity $object The entity itself * * @return true * @access private Set as private in 1.9.0 */ function metadata_update($event, $object_type, $object) { if ($object instanceof ElggEntity) { if (!is_metadata_independent($object->getType(), $object->getSubtype())) { $db_prefix = elgg_get_config('dbprefix'); $access_id = (int) $object->access_id; $guid = (int) $object->getGUID(); $query = "update {$db_prefix}metadata set access_id = {$access_id} where entity_guid = {$guid}"; update_data($query); } } return true; }
/** * Count the number of comments attached to an entity * * @param ElggEntity $entity * @return int Number of comments */ function elgg_count_comments($entity) { if ($commentno = trigger_plugin_hook('comments:count', $entity->getType(), array('entity' => $entity), false)) { return $commentno; } else { return count_annotations($entity->getGUID(), "", "", "generic_comment"); } }
/** * Return the icon URL for an entity. * * @tip Can be overridden by registering a plugin hook for entity:icon:url, $entity_type. * * @internal This is passed an entity rather than a guid to handle non-created entities. * * @param ElggEntity $entity The entity * @param string $size Icon size * * @return string URL to the entity icon. * @deprecated 1.8 Use $entity->getIconURL() */ function get_entity_icon_url(ElggEntity $entity, $size = 'medium') { elgg_deprecated_notice("get_entity_icon_url() deprecated for getIconURL()", 1.8); global $CONFIG; $size = sanitise_string($size); switch (strtolower($size)) { case 'master': $size = 'master'; break; case 'large': $size = 'large'; break; case 'topbar': $size = 'topbar'; break; case 'tiny': $size = 'tiny'; break; case 'small': $size = 'small'; break; case 'medium': default: $size = 'medium'; } $url = false; $viewtype = elgg_get_viewtype(); // Step one, see if anyone knows how to render this in the current view $params = array('entity' => $entity, 'viewtype' => $viewtype, 'size' => $size); $url = elgg_trigger_plugin_hook('entity:icon:url', $entity->getType(), $params, $url); // Fail, so use default if (!$url) { $type = $entity->getType(); $subtype = $entity->getSubtype(); if (!empty($subtype)) { $overrideurl = elgg_view("icon/{$type}/{$subtype}/{$size}", array('entity' => $entity)); if (!empty($overrideurl)) { return $overrideurl; } } $overrideurl = elgg_view("icon/{$type}/default/{$size}", array('entity' => $entity)); if (!empty($overrideurl)) { return $overrideurl; } $url = "_graphics/icons/default/{$size}.png"; } return elgg_normalize_url($url); }
/** * Check if the given entity is searchable (and needs to be in Elasticsearch) * * @param \ElggEntity $entity the entity * * @return bool */ protected static function isSearchableEntity(\ElggEntity $entity) { if (empty($entity) || !$entity instanceof \ElggEntity) { return false; } $type = $entity->getType(); $type_subtypes = elasticsearch_get_registered_entity_types(); if (!isset($type_subtypes[$type])) { return false; } $subtype = $entity->getSubtype(); if (empty($subtype)) { // eg. user, group, site return true; } return in_array($subtype, $type_subtypes[$type]); }