ed 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'); 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_link_template = em_add_get_params($page_url, $pag_args, 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($this->shortnames[$shortname]) ){ $property = $this->shortnames[$shortname]; return $this->{$property}; } return null; } public function __set($prop, $val ){ if( !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{ $this->{$prop} = $val; } } public function __isset( $prop ){ if( !empty($this->shortnames[$prop]) ){ $property = $this->shortnames[$prop]; return !empty($this->{$property}); } 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); } /** * 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()); } /** * Used to process any tables containing meta, such as bookings_meta or tickets_bookings_meta * This may likely be moved into another object, which children extend instead of this. If you choose to depend on this function, keep an eye out in future updates, you're best off copying the code for now * @param array $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]+)_/', $meta_key, $match) ){ $key = $match[1]; $subkey = str_replace('_'.$key.'_', '', $meta_key); if( empty($processed_meta[$key]) ) $processed_meta[$key] = array(); if( !empty($processed_meta[$key][$subkey]) ){ 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; } }