public static function load_MVC_files()
 {
     $dir = plugin_dir_path(__FILE__);
     // -- MODELS
     include_once $dir . 'inc/models/ESS_Database.php';
     include_once $dir . 'inc/models/ESS_Images.php';
     include_once $dir . 'inc/models/ESS_IO.php';
     include_once $dir . 'inc/models/ESS_Notices.php';
     include_once $dir . 'inc/models/ESS_Sounds.php';
     include_once $dir . 'inc/models/ESS_Timezone.php';
     include_once $dir . 'inc/models/ESS_Videos.php';
     // -- VIEWS
     include_once $dir . 'inc/views/ESS_Admin.php';
     include_once $dir . 'inc/views/ESS_Elements.php';
     include_once $dir . 'inc/views/ESS_Feed.php';
     // -- CONTROLLERS
     include_once $dir . 'inc/controllers/ESS_Control_admin.php';
     include_once $dir . 'inc/controllers/ESS_Import.php';
     if (class_exists('FeedWriter') == FALSE) {
         require_once EM_ESS_DIR . "/inc/libs/ess/FeedWriter.php";
     }
     ESS_Notices::set_notices_global_handler();
     ESS_IO::set_filters_handler();
     ESS_Database::set_default_values();
 }
 public static function add(array $DATA_ = NULL)
 {
     $url = $DATA_['uri'];
     $name = @$DATA_['name'];
     $post_id = @$DATA_['post_id'];
     apply_filters('em_object_get_image_type', 'event', false, isset($this) ? $this : self);
     //Handle the attachment as a WP Post
     $attachment = '';
     $tmpfname = tempnam(ESS_IO::tmp(), "ESS_IMG_" . $post_id . "_");
     $h = fopen($tmpfname, "w");
     if ($h && strlen($url) > 5) {
         fwrite($h, file_get_contents($url));
         $image_ = getimagesize($tmpfname);
         switch ($image_['mime']) {
             case "image/gif":
                 $mime = "gif";
                 break;
             case "image/jpeg":
                 $mime = "jpg";
                 break;
             case "image/png":
                 $mime = "png";
                 break;
             case "image/bmp":
                 $mime = "bmp";
                 break;
         }
         $file = array('tmp_name' => $tmpfname, 'name' => basename($tmpfname), 'type' => $image_[2], 'mime' => $mime, 'width' => $image_[0], 'height' => $image_[1], 'size' => filesize($tmpfname), 'error' => 0);
         //var_dump( $file );
         if (file_exists($file['tmp_name']) && ESS_Images::image_validator($file)) {
             require_once ABSPATH . "wp-admin" . '/includes/file.php';
             require_once ABSPATH . "wp-admin" . '/includes/image.php';
             $attachment = ESS_Images::handle_upload($file);
             if ($attachment) {
                 //echo "DEBUG: <b>". __CLASS__.":".__LINE__."</b>";
                 //var_dump( $attachment );
                 $attachment_id = wp_insert_attachment(array('post_mime_type' => $image_['mime'], 'post_title' => $name, 'post_content' => '', 'post_status' => 'inherit'), $attachment['file'], $post_id);
                 $attachment_metadata = wp_generate_attachment_metadata($attachment_id, $attachment['file']);
                 wp_update_attachment_metadata($attachment_id, $attachment_metadata);
                 update_post_meta($post_id, '_thumbnail_id', $attachment_id);
                 update_post_meta($post_id, '_start_ts', date('U'));
                 update_post_meta($post_id, '_end_ts', intval(date('U') + 60 * 60 * 24 * 365));
                 @fclose($h);
                 if (file_exists($tmpfname)) {
                     @unlink($tmpfname);
                 }
                 return apply_filters('em_object_image_upload', $attachment_id, $this);
             }
         }
         @fclose($h);
         if (file_exists($tmpfname)) {
             @unlink($tmpfname);
         }
     }
     return apply_filters('em_object_image_upload', false, $this);
 }
 public static function set_event_saved_filter($result = NULL, $event_id = NULL)
 {
     if ($result == TRUE) {
         if (empty($event_id)) {
             global $EM_Event;
         } else {
             $EM_Event = em_get_event($event_id);
         }
         if ($EM_Event instanceof EM_Event && intval($EM_Event->event_id) > 0) {
             $feed_url = ESS_IO::get_feed_url($EM_Event->event_id, FALSE, TRUE);
             return ESS_IO::get_curl_result(FeedWriter::$AGGREGATOR_WS, $feed_url) !== FALSE;
         }
     }
     return FALSE;
 }
 private static function get_ess_image($event_id = '')
 {
     return "<a href='" . ESS_IO::get_feed_url($event_id) . "' target='_blank' title='" . __('ESS Feed of all the events published in the website and available for syndication.', 'dbem') . "'>" . "<img src='" . EM_ESS_URL . "/assets/img/ESS_logo_16x16.png' title='" . __("Events content with ESS Feed format", 'dbem') . "' style='margin-right:6px;'/>" . "</a>";
 }
 /**
  * Simple test to check if the URL targets to an existing file.
  *
  * @param 	String	feed_url	URL of the ess feed file to test
  * @return	Boolean	result		return a boolean value.
  */
 public static function is_feed_valid($feed_url = '')
 {
     global $ESS_Notices;
     if (FeedValidator::isValidURL($feed_url) == FALSE) {
         $ESS_Notices->add_error(sprintf(__("The ESS URL is not valid: <a href='%s' target='_blank'>%s</a>", 'dbem'), $feed_url, $feed_url));
         return FALSE;
     } else {
         $response = json_decode(ESS_IO::get_curl_result(FeedWriter::$VALIDATOR_WS, $feed_url), TRUE);
         //dd( $response );
         if ($response !== FALSE) {
             $r = @$response['result'];
             //dd( $r );
             if (@isset($r['result'])) {
                 if (strlen(@$r['result']['error']) > 1 || @$r['type'] == 'KO') {
                     //d( strlen( @$r['result']['error'] ) );
                     if ($response['result']['error'] == NULL) {
                         $response['result']['error'] = array();
                     }
                     array_unshift($response['result']['error'], "<b>" . sprintf(__("The Feed URL is not a valide ESS file: <a href='%s' target='_blank'>%s</a>", 'dbem'), $feed_url, $feed_url) . "</b><br>");
                     array_push($response['result']['error'], "<b>" . sprintf(__("More information about the standard: <a href='%s' target='_blank'>%s</a>", 'dbem'), ESS_IO::ESS_WEBSITE, ESS_IO::ESS_WEBSITE) . "</b><br>");
                     //dd( $r );
                     $ESS_Notices->add_error($response);
                     return FALSE;
                 }
             }
             return TRUE;
         }
     }
     return FALSE;
 }