ery_vars['tax_query']) ) $wp_query->query_vars['tax_query'] = array(); $wp_query->query_vars['tax_query'] = array('taxonomy' => EM_TAXONOMY_CATEGORY, 'field'=>'id', 'terms'=>$term_ids); } } } //Add conditions for tags //Filter by tag, can be id or comma separated ids if ( !empty($tag) && !is_array($tag) ){ //get the term id directly $term = new EM_Tag($tag); if( !empty($term->term_id) ){ if( !is_array($wp_query->query_vars['tax_query']) ) $wp_query->query_vars['tax_query'] = array(); $wp_query->query_vars['tax_query'] = array('taxonomy' => EM_TAXONOMY_TAXONOMY, 'field'=>'id', 'terms'=>$term->term_taxonomy_id); } }elseif( is_array($tag) ){ $term_ids = array(); foreach($tag as $tag_data){ $term = new EM_Tag($tag_data); if( !empty($term->term_id) ){ $term_ids[] = $term->term_taxonomy_id; } } if( count($term_ids) > 0 ){ if( !is_array($wp_query->query_vars['tax_query']) ) $wp_query->query_vars['tax_query'] = array(); $wp_query->query_vars['tax_query'] = array('taxonomy' => EM_TAXONOMY_TAXONOMY, 'field'=>'id', 'terms'=>$term_ids); } } //If we want rsvped items, we usually check the event if( $args['bookings'] == 1 ){ $query[] = array( 'key' => '_event_rsvp', 'value' => 1, 'compare' => '=' ); } //Default ownership belongs to an event, child objects can just overwrite this if needed. if( is_numeric($owner) ){ $wp_query->query_vars['author'] = $owner; }elseif( $owner == 'me' && is_user_logged_in() ){ $wp_query->query_vars['author'] = get_current_user_id(); } if( !empty($query) && is_array($query) ){ $wp_query->query_vars['meta_query'] = $query; } return apply_filters('em_object_build_wp_query_conditions', $wp_query); } /** * Sanitizes the ORDER BY part of the SQL statement so only valid fields are supplied for ordering. * Also combines default orders which can be an array which is applied to each specific ordering field, or just one value applied to all ordering fields. * @uses EM_Object::build_sql_x_by_helper() * @param array $args * @param array $accepted_fields * @param string|array $default_order * @return array */ public static function build_sql_orderby( $args, $accepted_fields, $default_order = 'ASC' ){ //First, ORDER BY $args = apply_filters('em_object_build_sql_orderby_args', $args, $accepted_fields, $default_order ); $orderby = self::build_sql_x_by_helper($args['orderby'], $args['order'], $accepted_fields, $default_order); return apply_filters('em_object_build_sql_orderby', $orderby, $args, $accepted_fields, $default_order ); } /** * Returns a set of further fields this query should be grouped by. Not required for straight-forward GROUP BY SQL queries. * This is supplementary for build_sql_groupby in cases such as events, where ordering and grouping are mixed due to complex SQL sub-queries and partitions. * @uses EM_Object::build_sql_x_by_helper() * @param unknown $args * @param unknown $accepted_fields * @param string $default_order * @return mixed|unknown */ public static function build_sql_groupby_orderby( $args, $accepted_fields, $default_order = 'ASC' ){ $args = apply_filters('em_object_build_sql_groupby_orderby_args', $args, $accepted_fields, $default_order ); $orderby = self::build_sql_x_by_helper($args['groupby_orderby'], $args['groupby_order'], $accepted_fields, $default_order); return apply_filters('em_object_build_sql_groupby_orderby', $orderby, $args, $accepted_fields, $default_order ); } /** * Sanitizes the group by statement so it includes only accepted fields. Returns an array of valid field names to group by. * Optionally, if a $groupby_order value is provided then ASC/DESC values will be added to each field similar to EM_Object::build_sql_orderby * @uses EM_Object::build_sql_x_by_helper() * @param unknown $args * @param unknown $accepted_fields * @param string $groupby_order * @param string $default_order * @return mixed|unknown */ public static function build_sql_groupby( $args, $accepted_fields, $groupby_order = false, $default_order = 'ASC' ){ //First, ORDER BY $args = apply_filters('em_object_build_sql_groupby_args', $args); $groupby = self::build_sql_x_by_helper($args['groupby'], $groupby_order, $accepted_fields, $default_order); return apply_filters('em_object_build_sql_groupby', $groupby, $args, $accepted_fields); } /** * Helper for building arrays of fields * @param unknown $x_by_field * @param unknown $order * @param unknown $accepted_fields * @param string $default_order * @return array */ protected static function build_sql_x_by_helper($x_by_field, $order, $accepted_fields, $default_order = 'ASC' ){ $x_by = array(); if(is_array($x_by_field)){ //Clean orderby array so we only have accepted values foreach( $x_by_field as $key => $field ){ if( array_key_exists($field, $accepted_fields) ){ //maybe cases we're given an array where keys are shortcut names e.g. id => event_id - this way will be deprecated at one point $x_by[] = $accepted_fields[$field]; }elseif( in_array($field,$accepted_fields) ){ $x_by[] = $field; }else{ unset($x_by[$key]); } } }elseif( $x_by_field != '' && array_key_exists($x_by_field, $accepted_fields) ){ $x_by[] = $accepted_fields[$x_by_field]; }elseif( $x_by_field != '' && in_array($x_by_field, $accepted_fields) ){ $x_by[] = $x_by_field; } //ORDER if( $order !== false ){ foreach($x_by as $i => $field){ $x_by[$i] .= ' '; if(is_array($order)){ //If order is an array, we'll go through the orderby array and match the order values (in order of array) with orderby values if( in_array($order[$i], array('ASC','DESC','asc','desc')) ){ $x_by[$i] .= $order[$i]; }else{ //If orders don't match up, or it's not ASC/DESC, the default events search in EM settings/options page will be used. $x_by[$i] .= $default_order; } }else{ $x_by[$i] .= ( in_array($order, array('ASC','DESC','asc','desc')) ) ? $order : $default_order; } } } return $x_by; } /** * Fixes ambiguous fields in a given array (which can contain prefixed ASC/DESC arguments) and give them scope of events table * @param array $fields * @return array */ protected static function build_sql_ambiguous_fields_helper( $fields, $reserved_fields = array(), $prefix = 'table_name' ){ foreach($fields as $k => $v){ $needle = trim(str_replace(array('ASC','DESC'), '', $v)); //remove ASC DESC for searching/comparison arrays such as order by if( in_array($needle, $reserved_fields) ){ $fields[$k] = $prefix.'.'.$v; } } return $fields; } /** * Gets array of searchable variables that should be considered in a $_REQUEST variable * @param array $args Arguments to include in returned array * @param string $filter Filters out any unrecognized arguments already passed into $args * @param array $request defaults to $_REQUEST if empty but can be an array of items to go through instead * @param array $accepted_searches defaults to EM_Object::get_search_defaults(), objects should call self::get_search_defaults() to get around late static binding problems * @return array */ public static function get_post_search($args = array(), $filter = false, $request = array(), $accepted_searches = array()){ if( empty($request) ) $request = $_REQUEST; if( !empty($request['em_search']) && empty($args['search']) ) $request['search'] = $request['em_search']; //em_search is included to circumvent wp search GET/POST clashes $accepted_searches = !empty($accepted_searches) ? $accepted_searches : static::get_default_search(); $accepted_searches = array_diff($accepted_searches, array('format', 'format_header', 'format_footer')); $accepted_searches = apply_filters('em_accepted_searches', $accepted_searches, $args); //merge variables from the $request into $args foreach($request as $post_key => $post_value){ if( in_array($post_key, $accepted_searches) && !empty($post_value) ){ if(is_array($post_value)){ $post_value = implode(',',$post_value); }else{ $post_value = wp_unslash($post_value); } if($post_value != ',' ){ $args[$post_key] = $post_value; }elseif( $post_value == ',' && $post_key == 'scope' && (empty($args['scope']) || $args['scope'] == array('','')) ){ //unset the scope if no value is provided - ',' is an empty value unset($args['scope']); } } } if( $filter ){ foreach($args as $arg_key => $arg_value){ if( !in_array($arg_key, $accepted_searches) ){ unset($args[$arg_key]); } } } return apply_filters('em_get_post_search', $args); } /** * Generates pagination for classes like EM_Events based on supplied arguments and whether AJAX is enabled. * * @param array $args The arguments being searched for * @param integer $count The number of total items to paginate through * @param string $search_action The name of the action query var used to trigger a search - used in AJAX requests and normal searches * @param array $default_args The default arguments and values this object accepts, used to compare against $args to create a querystring * @return string * @uses em_paginate() */ public static function get_pagination_links($args, $count, $search_action = 'search_events', $default_args = array()){ $limit = ( !empty($args['limit']) && is_numeric($args['limit']) ) ? $args['limit']:false; $page = ( !empty($args['page']) && is_numeric($args['page']) ) ? $args['page']:1; $pno = !empty($args['page_queryvar']) ? $args['page_queryvar'] : 'pno'; $default_pag_args = array($pno=>'%PAGE%', 'page'=>null, 'search'=>null, 'action'=>null, 'pagination'=>null); //clean out the bad stuff, set up page number template $page_url = $_SERVER['REQUEST_URI']; //$default_args are values that can be added to the querystring for use in searching events in pagination either in searches or ajax pagination if( !empty($_REQUEST['action']) && $_REQUEST['action'] == $search_action && empty($default_args) ){ //due to late static binding issues in PHP, this'll always return EM_Object::get_default_search so this is a fall-back $default_args = static::get_default_search(); } //go through default arguments (if defined) and build a list of unique non-default arguments that should go into the querystring $unique_args = array(); //this is the set of unique arguments we'll add to the querystring $ignored_args = array('offset', 'ajax', 'array', 'pagination','format','format_header','format_footer','page'); foreach( $default_args as $arg_key => $arg_default_val){ if( array_key_exists($arg_key, $args) && !in_array($arg_key, $ignored_args) ){ //if array exists, implode it in case one value is already imploded for matching purposes $arg_val = is_array($args[$arg_key]) ? implode(',', $args[$arg_key]) : $args[$arg_key]; $arg_default_val = is_array($arg_default_val) ? implode(',',$arg_default_val) : $arg_default_val; if( $arg_val != $arg_default_val ){ $unique_args[$arg_key] = $arg_val; } } } if( !empty($unique_args['search']) ){ $unique_args['em_search'] = $unique_args['search']; //special case, since em_search is used in links rather than search, which we remove below unset($unique_args['search']); } //build general page link with all arguments $pag_args = array_merge($unique_args, $default_pag_args); //if we're using ajax or already did an events search via a form, add the action here for pagination links if( !empty($args['ajax']) || (!empty($_REQUEST['action']) && $_REQUEST['action'] == $search_action ) ){ $unique_args['action'] = $pag_args['action'] = $search_action; } //if we're in an ajax call, make sure we aren't calling admin-ajax.php if( defined('DOING_AJAX') ) $page_url = em_wp_get_referer(); //finally, glue the url with querystring and pass onto pagination function $page_args_escaped = array(); foreach( $pag_args as $key => $val ){ $page_args_escaped[$key] = $val && is_string($val) ? urlencode($val) : $val; } $page_link_template = em_add_get_params($page_url, $page_args_escaped, false, false); //don't html encode, so em_paginate does its thing; //if( empty($args['ajax']) || defined('DOING_AJAX') ) $unique_args = array(); //don't use data method if ajax is disabled or if we're already in an ajax request (SERP irrelevenat) $return = apply_filters('em_object_get_pagination_links', em_paginate( $page_link_template, $count, $limit, $page, $unique_args, !empty($args['ajax']) ), $page_link_template, $count, $limit, $page); //if PHP is 5.3 or later, you can specifically filter by class e.g. em_events_output_pagination - this replaces the old filter originally located in the actual child classes if( function_exists('get_called_class') ){ $return = apply_filters(strtolower(get_called_class()).'_output_pagination', $return, $page_link_template, $count, $limit, $page); } return $return; } public function __get( $shortname ){ if ( !empty(static::$field_shortcuts[$shortname]) ){ $property = static::$field_shortcuts[$shortname]; return $this->{$property}; } elseif ( !empty($this->shortnames[$shortname]) ){ $property = $this->shortnames[$shortname]; return $this->{$property}; } elseif ( isset($this->dynamic_properties[$shortname]) ) { return $this->dynamic_properties[$shortname]; } return null; } /** * Sets a property of this object, either in the object itself or in the dynamic_properties array. Accepts shortcut names for properties as mapped by the $field_shortcuts or $shortnames array. * @param string $prop * @param mixed $val */ public function __set($prop, $val ){ if ( !empty(static::$field_shortcuts[$prop]) ) { $property = static::$field_shortcuts[$prop]; if( !empty($this->fields[$property]['type']) && $this->fields[$property]['type'] == '%d' ){ $val = absint($val); } $this->{$property} = $val; } elseif ( !empty($this->shortnames[$prop]) ) { $property = $this->shortnames[$prop]; if( !empty($this->fields[$property]['type']) && $this->fields[$property]['type'] == '%d' ){ $val = absint($val); } $this->{$property} = $val; } else { // save it to an declared array property to avoid 8.2 errors, access it the same way $this->dynamic_properties[$prop] = $val; } } /** * Checks if a property has been set, either in the object itself or in the dynamic_properties array. * @param string $prop * @return boolean */ public function __isset( $prop ){ if( !empty($this->shortnames[$prop]) ){ $property = $this->shortnames[$prop]; return !empty($this->{$property}); } elseif ( isset($this->dynamic_properties[$prop]) ) { return isset($this->dynamic_properties[$prop]); } return !empty($this->{$prop}); } /** * Returns the id of a particular object in the table it is stored, be it Event (event_id), Location (location_id), Tag, Booking etc. * @return int */ function get_id(){ switch( get_class($this) ){ case 'EM_Event': return $this->event_id; case 'EM_Location': return $this->location_id; case 'EM_Category': return $this->term_id; case 'EM_Tag': return $this->term_id; case 'EM_Ticket': return $this->ticket_id; case 'EM_Ticket_Booking': return $this->ticket_booking_id; } return 0; } /** * Returns the user id for the owner (author) of a particular object in the table it is stored, be it Event (event_owner) or Location (location_owner). * This function accounts for the fact that previously the property $this->owner was used by objects as a shortcut and consequently in code in EM_Object, which should now use this method instead. * Extending classes should override this and provide the relevant user id that owns this object instance. * @return int */ function get_owner(){ if( !empty($this->owner) ) return $this->owner; switch( get_class($this) ){ case 'EM_Event': return $this->event_owner; case 'EM_Location': return $this->location_owner; } return 0; } /** * Used by "single" objects, e.g. bookings, events, locations to verify if they have the capability to edit this or someone else's object. Relies on the fact that the object has an owner property with id of user (or admin capability must pass). * @param string $owner_capability If the object has an owner property and the user id matches that, this capability will be checked for. * @param string $admin_capability If the user isn't the owner of the object, this capability will be checked for. * @return boolean */ function can_manage( $owner_capability = false, $admin_capability = false, $user_to_check = false ){ global $em_capabilities_array; //if multisite and super admin, just return true if( is_multisite() && em_wp_is_super_admin() ){ return true; } //set user to the desired user we're verifying, otherwise default to current user if( $user_to_check ){ $user = new WP_User($user_to_check); } if( empty($user->ID) ) $user = wp_get_current_user(); //do they own this? $owner_id = $this->get_owner(); $is_owner = ( (!empty($owner_id) && ($owner_id == get_current_user_id()) || !$this->get_id() || (!empty($user) && $owner_id == $user->ID)) ); //now check capability $can_manage = false; if( $is_owner && $owner_capability && $user->has_cap($owner_capability) ){ //user owns the object and can therefore manage it $can_manage = true; }elseif( $owner_capability && array_key_exists($owner_capability, $em_capabilities_array) ){ //currently user is not able to manage as they aren't the owner $error_msg = $em_capabilities_array[$owner_capability]; } //admins have special rights if( !$admin_capability ) $admin_capability = $owner_capability; if( $admin_capability && $user->has_cap($admin_capability) ){ $can_manage = true; }elseif( $admin_capability && array_key_exists($admin_capability, $em_capabilities_array) ){ $error_msg = $em_capabilities_array[$admin_capability]; } $can_manage = apply_filters('em_object_can_manage', $can_manage, $this, $owner_capability, $admin_capability, $user_to_check); if( !$can_manage && !$is_owner && !empty($error_msg) ){ $this->add_error($error_msg); } return $can_manage; } public static function ms_global_switch(){ if( EM_MS_GLOBAL ){ //If in multisite global, then get the main blog global $current_site; switch_to_blog($current_site->blog_id); } } public static function ms_global_switch_back(){ if( EM_MS_GLOBAL ){ restore_current_blog(); } } /** * Save an array into this class. * If you provide a record from the database table corresponding to this class type it will add the data to this object. * @param array $array * @return null */ function to_object( $array = array(), $addslashes = false ){ //Save core data if( is_array($array) ){ $array = apply_filters('em_to_object', $array); foreach ( array_keys($this->fields) as $key ) { if(array_key_exists($key, $array)){ if( !is_object($array[$key]) && !is_array($array[$key]) ){ $array[$key] = ($addslashes) ? wp_unslash($array[$key]):$array[$key]; }elseif( is_array($array[$key]) ){ $array[$key] = ($addslashes) ? wp_unslash_deep($array[$key]):$array[$key]; } $this->$key = $array[$key]; } } } } /** * Copies all the properties to shorter property names for compatability, do not use the old properties. */ function compat_keys(){ foreach($this->fields as $key => $fieldinfo){ if( !empty($fieldinfo['name']) ){ $field_name = $fieldinfo['name']; if(!empty($this->$key)) $this->$field_name = $this->$key; } } } /** * Returns this object in the form of an array, useful for saving directly into a database table. * @return array */ function to_array($db = false){ $array = array(); foreach ( $this->fields as $key => $val ) { if($db){ if( !empty($this->$key) || $this->$key === 0 || $this->$key === '0' || empty($val['null']) ){ $array[$key] = $this->$key; }elseif( $this->$key === null && !empty($val['null']) ){ $array[$key] = null; } }else{ $array[$key] = $this->$key; } } return apply_filters('em_to_array', $array); } /** * Function to retreive wpdb types for all fields, or if you supply an assoc array with field names as keys it'll return an equivalent array of wpdb types * @param array $array * @return array: */ function get_types($array = array()){ $types = array(); if( count($array)>0 ){ //So we look at assoc array and find equivalents foreach ($array as $key => $val){ $types[] = $this->fields[$key]['type']; } }else{ //Blank array, let's assume we're getting a standard list of types foreach ($this->fields as $field){ $types[] = $field['type']; } } return apply_filters('em_object_get_types', $types, $this, $array); } function get_fields( $inverted_array=false ){ if( is_array($this->fields) ){ $return = array(); foreach($this->fields as $fieldName => $fieldArray){ if($inverted_array){ if( !empty($fieldArray['name']) ){ $return[$fieldArray['name']] = $fieldName; }else{ $return[$fieldName] = $fieldName; } }else{ $return[$fieldName] = $fieldArray['name']; } } return apply_filters('em_object_get_fields', $return, $this, $inverted_array); } return apply_filters('em_object_get_fields', array(), $this, $inverted_array); } /** * Cleans arrays that contain id lists. Takes an array of items and will clean the keys passed in second argument so that if they keep numbers, explode comma-separated numbers, and unsets the key if there's any other value * @param array $array * @param array $id_atts */ public static function clean_id_atts( $array = array(), $id_atts = array() ){ if( is_array($array) && is_array($id_atts) ){ foreach( $array as $key => $string ){ if( in_array($key, $id_atts) ){ //This is in the list of atts we want cleaned if( is_numeric($string) ){ $array[$key] = (int) $string; }elseif( self::array_is_numeric($string) ){ $array[$key] = $string; }elseif( !is_array($string) && preg_match('/^( ?[\-0-9] ?,?)+$/', $string) ){ $array[$key] = explode(',', str_replace(' ','',$string)); }else{ //No format we accept unset($array[$key]); } } } } return $array; } /** * Send an email and log errors in this object * @param string $subject * @param string $body * @param string $email * @param array $attachments * @param array $args * @return string */ function email_send($subject, $body, $email, $attachments = array(), $args = array() ){ global $EM_Mailer; if( !empty($subject) ){ if( !is_object($EM_Mailer) ){ $EM_Mailer = new EM_Mailer(); } if( !$EM_Mailer->send($subject,$body,$email, $attachments, $args) ){ if( is_array($EM_Mailer->errors) ){ foreach($EM_Mailer->errors as $error){ $this->errors[] = $error; } }else{ $this->errors[] = $EM_Mailer->errors; } return false; } } return true; } /** * Will return true if this is a simple (non-assoc) numeric array, meaning it has at one or more numeric entries and nothing else * @param mixed $array * @return boolean */ public static function array_is_numeric($array){ $results = array(); if(is_array($array)){ foreach($array as $key => $item){ $results[] = (is_numeric($item)&&is_numeric($key)); } } return (!in_array(false, $results) && count($results) > 0); } /** * Checks and returns a sanitized array of integer numbers * @param $array * * @return array */ public static function sanitize_numeric_array( $array ) { $sanitized_array = array(); if ( !is_array( $array ) ) { $array = explode( ',', $array ); } if( static::array_is_numeric( $array ) ) { foreach ( $array as $key => $value ) { if ( is_numeric( $value ) ) { $sanitized_array[ $key ] = intval($value); } } } return $sanitized_array; } /** * Returns an array of errors in this object * @return array */ function get_errors(){ if(is_array($this->errors)){ return $this->errors; }else{ return array(); } } /** * Adds an error to the object */ function add_error($errors){ if(!is_array($errors)){ $errors = array($errors); } //make errors var an array if it isn't already if(!is_array($this->errors)){ $this->errors = array(); } //create empty array if this isn't an array foreach($errors as $key => $error){ if( !in_array($error, $this->errors) ){ if( !is_array($error) ){ $this->errors[] = $error; }else{ $this->errors[] = array($key => $error); } } } } /** * Converts an array to JSON format, useful for outputting data for AJAX calls. Uses a PHP4 fallback function, given it doesn't support json_encode(). * @param array $array * @return string */ public static function json_encode($array){ $array = apply_filters('em_object_json_encode_pre',$array); if( function_exists("json_encode") ){ $return = json_encode($array); }else{ $return = self::array_to_json($array); } if( isset($_REQUEST['callback']) && preg_match("/^jQuery[_a-zA-Z0-9]+$/", $_REQUEST['callback']) ){ $return = $_REQUEST['callback']."($return)"; } return apply_filters('em_object_json_encode', $return, $array); } /** * Outputs array as JSON format as per EM_Object::json_encode() * @param $array * * @return void * @see EM_Object::json_encode() */ public static function json_encode_e($array){ echo static::json_encode($array); } /** * Compatible json encoder function for PHP4 * @param array $array * @return string */ function array_to_json($array){ //PHP4 Comapatability - This encodes the array into JSON. Thanks go to Andy - http://www.php.net/manual/en/function.json-encode.php#89908 if( !is_array( $array ) ){ $array = array(); } $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) )); if( $associative ){ $construct = array(); foreach( $array as $key => $value ){ // We first copy each key/value pair into a staging array, // formatting each key and value properly as we go. // Format the key: if( is_numeric($key) ){ $key = "key_$key"; } $key = "'".addslashes($key)."'"; // Format the value: if( is_array( $value )){ $value = self::array_to_json( $value ); }else if( is_bool($value) ) { $value = ($value) ? "true" : "false"; }else if( !is_numeric( $value ) || is_string( $value ) ){ $value = "'".addslashes($value)."'"; } // Add to staging array: $construct[] = "$key: $value"; } // Then we collapse the staging array into the JSON form: $result = "{ " . implode( ", ", $construct ) . " }"; } else { // If the array is a vector (not associative): $construct = array(); foreach( $array as $value ){ // Format the value: if( is_array( $value )){ $value = self::array_to_json( $value ); } else if( !is_numeric( $value ) || is_string( $value ) ){ $value = "'".addslashes($value)."'"; } // Add to staging array: $construct[] = $value; } // Then we collapse the staging array into the JSON form: $result = "[ " . implode( ", ", $construct ) . " ]"; } return $result; } /* * START IMAGE UPlOAD FUNCTIONS * Used for various objects, so shared in one place */ /** * Returns the type of image in lowercase, if $path is true, a base filename is returned which indicates where to store the file from the root upload folder. * @param unknown_type $path * @return mixed|mixed */ function get_image_type($path = false){ $type = false; switch( get_class($this) ){ case 'EM_Event': $dir = (EM_IMAGE_DS == '/') ? 'events/':''; $type = 'event'; break; case 'EM_Location': $dir = (EM_IMAGE_DS == '/') ? 'locations/':''; $type = 'location'; break; case 'EM_Category': $dir = (EM_IMAGE_DS == '/') ? 'categories/':''; $type = 'category'; break; } if($path){ return apply_filters('em_object_get_image_type',$dir.$type, $path, $this); } return apply_filters('em_object_get_image_type',$type, $path, $this); } function get_image_url($size = 'full'){ $image_url = $this->image_url; if( !empty($this->post_id) && (empty($this->image_url) || $size != 'full') ){ $post_thumbnail_id = get_post_thumbnail_id( $this->post_id ); $src = wp_get_attachment_image_src($post_thumbnail_id, $size); if( !empty($src[0]) && $size == 'full' ){ $image_url = $this->image_url = $src[0]; }elseif(!empty($src[0])){ $image_url = $src[0]; } //legacy image finder, annoying, but must be done if( empty($image_url) ){ $type = $this->get_image_type(); if( get_class($this) == "EM_Event" ){ $id = ( $this->is_recurrence() ) ? $this->recurrence_id:$this->event_id; //quick fix for recurrences }elseif( get_class($this) == "EM_Location" ){ $id = $this->location_id; }else{ $id = $this->id; } if( $type ){ foreach($this->mime_types as $mime_type) { $file_name = $this->get_image_type(true)."-{$id}.$mime_type"; if( file_exists( EM_IMAGE_UPLOAD_DIR . $file_name) ) { $image_url = $this->image_url = EM_IMAGE_UPLOAD_URI.$file_name; } } } } } return apply_filters('em_object_get_image_url', $image_url, $this); } function image_delete($force_delete=true) { $type = $this->get_image_type(); if( $type ){ $this->image_url = ''; if( $this->get_image_url() == '' ){ $result = true; }else{ $post_thumbnail_id = get_post_thumbnail_id( $this->post_id ); //check that this image isn't being used by another CPT global $wpdb; $sql = $wpdb->prepare('SELECT count(*) FROM '.$wpdb->postmeta." WHERE meta_key='_thumbnail_id' AND meta_value=%d", array($post_thumbnail_id)); if( $wpdb->get_var($sql) <= 1 ){ //not used by any other CPT, so just delete the image entirely (would usually only be used via front-end which has no media manager) //@todo add setting option to delete images from filesystem/media if not used by other posts $delete_attachment = wp_delete_attachment($post_thumbnail_id, $force_delete); if( false === $delete_attachment ){ //check legacy image $type_id_name = $type.'_id'; $file_name= EM_IMAGE_UPLOAD_DIR.$this->get_image_type(true)."-".$this->$type_id_name; $result = false; foreach($this->mime_types as $mime_type) { if (file_exists($file_name.".".$mime_type)){ $result = unlink($file_name.".".$mime_type); } } }else{ $result = true; } }else{ //just delete image association delete_post_meta($this->post_id, '_thumbnail_id'); } } } return apply_filters('em_object_get_image_url', $result, $this); } function image_upload(){ $type = $this->get_image_type(); //Handle the attachment as a WP Post $attachment = ''; $user_to_check = ( !is_user_logged_in() && get_option('dbem_events_anonymous_submissions') ) ? get_option('dbem_events_anonymous_user'):false; if ( !empty($_FILES[$type.'_image']['size']) && file_exists($_FILES[$type.'_image']['tmp_name']) && $this->image_validate() && $this->can_manage('upload_event_images','upload_event_images', $user_to_check) ) { require_once(ABSPATH . "wp-admin" . '/includes/file.php'); require_once(ABSPATH . "wp-admin" . '/includes/image.php'); require_once( ABSPATH . 'wp-admin/includes/media.php' ); $attachment_id = media_handle_upload( $type.'_image', $this->post_id ); /* Attach file to item */ if ( !is_wp_error($attachment_id) ){ //delete the old attachment $this->image_delete(); update_post_meta($this->post_id, '_thumbnail_id', $attachment_id); return apply_filters('em_object_image_upload', true, $this); }else{ //error uploading, pass error message on and return false $error_string = __('There was an error uploading the image.','events-manager'); if( current_user_can('edit_others_events') && !empty($attachment_id->errors['upload_error']) ){ $error_string .= ' ('. implode(' ', $attachment_id->errors['upload_error']) .')'; } $this->add_error( $error_string ); return apply_filters('em_object_image_upload', false, $this); } }elseif( !empty($_REQUEST[$type.'_image_delete']) ){ $this->image_delete(); } return apply_filters('em_object_image_upload', false, $this); } function image_validate(){ $type = $this->get_image_type(); if( $type ){ if ( !empty($_FILES[$type.'_image']) && $_FILES[$type.'_image']['size'] > 0 ) { if (is_uploaded_file($_FILES[$type.'_image']['tmp_name'])) { list($width, $height, $mime_type, $attr) = getimagesize($_FILES[$type.'_image']['tmp_name']); $maximum_size = get_option('dbem_image_max_size'); if ($_FILES[$type.'_image']['size'] > $maximum_size){ $this->add_error( __('The image file is too big! Maximum size:', 'events-manager')." $maximum_size"); } $maximum_width = get_option('dbem_image_max_width'); $maximum_height = get_option('dbem_image_max_height'); $minimum_width = get_option('dbem_image_min_width'); $minimum_height = get_option('dbem_image_min_height'); if (($width > $maximum_width) || ($height > $maximum_height)) { $this->add_error( __('The image is too big! Maximum size allowed:','events-manager')." $maximum_width x $maximum_height"); } if (($width < $minimum_width) || ($height < $minimum_height)) { $this->add_error( __('The image is too small! Minimum size allowed:','events-manager')." $minimum_width x $minimum_height"); } if ( empty($mime_type) || !array_key_exists($mime_type, $this->mime_types) ){ $this->add_error(__('The image is in a wrong format!','events-manager')); } } } } return apply_filters('em_object_image_validate', count($this->errors) == 0, $this); } /* * END IMAGE UPlOAD FUNCTIONS */ function output_excerpt($excerpt_length = 55, $excerpt_more = '[...]', $cut_excerpt = true){ if( !empty($this->post_excerpt) ){ $replace = $this->post_excerpt; }else{ $replace = $this->post_content; } if( empty($this->post_excerpt) || $cut_excerpt ){ if ( preg_match('//', $replace, $matches) ) { $content = explode($matches[0], $replace, 2); $replace = force_balance_tags($content[0]); } if( !empty($excerpt_length) ){ //shorten content by supplied number - copied from wp_trim_excerpt $replace = strip_shortcodes( $replace ); $replace = str_replace(']]>', ']]>', $replace); $replace = wp_trim_words( $replace, $excerpt_length, $excerpt_more ); } } return $replace; } function sanitize_time( $time ){ if( !empty($time) && preg_match ( '/^([01]?\d|2[0-3]):([0-5]\d) ?(AM|PM)?$/', $time, $match ) ){ if( !empty($match[3]) && $match[3] == 'PM' && $match[1] != 12 ){ $match[1] = 12+$match[1]; }elseif( !empty($match[3]) && $match[3] == 'AM' && $match[1] == 12 ){ $match[1] = '00'; } $time = $match[1].":".$match[2].":00"; return $time; } return '00:00:00'; } /** * Formats a price according to settings and currency * @param double $price * @return string */ function format_price( $price ){ return em_get_currency_formatted( $price ); } /** * Returns contextual tax rate of object, which may be global or instance-specific. By default a number representing percentage is provided, e.g. 21% returns 21. * If $decimal is set to true, 21% is returned as 0.21 * @param boolean $decimal If set to true, a decimal representation will be returned. * @return float */ function get_tax_rate( $decimal = false ){ $tax_rate = get_option('dbem_bookings_tax'); $tax_rate = ($tax_rate > 0) ? $tax_rate : 0; if( $decimal && $tax_rate > 0 ) $tax_rate = $tax_rate / 100; return $tax_rate; } /** * Untility function, generates a UUIDv4 without dashes. * @return string */ function generate_uuid(){ return str_replace('-', '', wp_generate_uuid4()); } /** * Process meta stored in a meta table. Meta stored without a preceding _ are considered as-is values and added as a key/value pair to returned meta array, otherwise they are considered as arrays and parsed accordingly, so that each array item is stored as a separate row in the database and rebuilt when loaded. * * Prior to EM 6.4.5.1, arrays were stored by delimiting the key and subkey by an underscore, but this will cause issues if the key itself contains an underscore. As of 6.5 a keys will be delimited by a pipe instead, to allow for underscore use as needed. * Backward compatibility is still supported for older records that weren't stored with a pipe and the last underscore is considered the delimiter for the subkey. Developers should ensure if they have keys and subkeys with underscores to find a way to migrate that info with SQL to add a pipe in between. * This decision was taken because tickets in our core code do not have any subkeys contaning underscores, therefore considered a 'safe' approach. * * @param $raw_meta * * @return array */ function process_meta( $raw_meta ){ $processed_meta = array(); foreach( $raw_meta as $meta ){ $meta_value = maybe_unserialize($meta['meta_value']); $meta_key = $meta['meta_key']; if( preg_match('/^_([a-zA-Z\-0-9 _]+)\|([a-zA-Z\-0-9 _]+)?$/', $meta_key, $match) || preg_match('/^_([a-zA-Z\-0-9 _]+)_([^_]+)$/', $meta_key, $match) ){ $key = $match[1]; if( empty($processed_meta[$key]) ) $processed_meta[$key] = array(); $subkey = isset($match[2]) ? $match[2] : count($processed_meta[$key]); // allows for storing arrays without a key, such as _beverage_choice| can be stored multiple times in a row if key is not relevant if( !empty($processed_meta[$key][$subkey]) && preg_match('/\|$/', $meta_key) ){ if( !is_array($processed_meta[$key][$subkey]) ) { $processed_meta[$key][$subkey] = array($processed_meta[$key][$subkey]); } $processed_meta[$key][$subkey][] = $meta_value; }else{ $processed_meta[$key][$subkey] = $meta_value; } }else{ $processed_meta[$meta_key] = $meta_value; } } return $processed_meta; } }