/** * Action inserts/updates posts */ function cvtx_spd_insert_post($post_id, $post = null) { global $cvtx_spd_types; if (in_array($post->post_type, array_keys($cvtx_spd_types)) && !($post != null && $post->pinged == '12345')) { if ($post->post_type == 'cvtx_aeantrag' && isset($_POST['cvtx_aeantrag_antrag']) && isset($_POST['cvtx_aeantrag_zeile'])) { // get top and antrag and validate data $top_id = get_post_meta($_POST['cvtx_aeantrag_antrag'], 'cvtx_antrag_top', true); $top_ord = get_post_meta($top_id, 'cvtx_top_ord', true); $antrag_ord = get_post_meta($_POST['cvtx_aeantrag_antrag'], 'cvtx_antrag_ord', true); $event_id = get_post_meta($_POST['cvtx_aeantrag_antrag'], 'cvtx_antrag_event', false); $event_id = is_array($event_id) ? array_pop($event_id) : $event_id; $event_year = get_post_meta($event_id, 'cvtx_event_year', true); $event_nr = get_post_meta($event_id, 'cvtx_event_ord', true); // get page nr if (isset($_POST['cvtx_aeantrag_seite'])) { $aeantrag_page = $_POST['cvtx_aeantrag_seite']; } else { $aeantrag_page = false; } // fetch main info on line (first few numbers) if (preg_match('/^([0-9]+)/', $_POST['cvtx_aeantrag_zeile'], $match1)) { $aeantrag_zeile = $match1[1]; } else { $aeantrag_zeile = 0; } // look for vari-ending (some kind of -2 at the end of the line field) if (preg_match('/([0-9]+)$/', $_POST['cvtx_aeantrag_zeile'], $match2) && strlen($match2[1]) < strlen($_POST['cvtx_aeantrag_zeile'])) { $aeantrag_vari = $match2[1]; } else { $aeantrag_vari = false; } // get globally sortable string $_POST['cvtx_sort'] = cvtx_get_sort('cvtx_aeantrag', $data = array('top' => $top_ord, 'subject' => $antrag_ord, 'page' => $aeantrag_page, 'zeile' => $aeantrag_zeile, 'vari' => $aeantrag_vari, 'year' => $event_year, 'event' => $event_nr)); } foreach ($cvtx_spd_types[$post->post_type] as $key) { if (isset($_POST[$key])) { // save data $value = @$_POST[$key]; if (empty($value)) { delete_post_meta($post_id, $key); continue; } // If value is a string it should be unique if (!is_array($value)) { // Update meta if (!update_post_meta($post_id, $key, $value)) { // Or add the meta data add_post_meta($post_id, $key, $value); } } else { // If passed along is an array, we should remove all previous data delete_post_meta($post_id, $key); // Loop through the array adding new values to the post meta as different entries with the same name foreach ($value as $entry) { add_post_meta($post_id, $key, $entry); } } } } } }
/** * Action inserts/updates posts */ function cvtx_insert_post($post_id, $post = null) { global $cvtx_types; global $cvtx_allowed_image_types; $options = get_option('cvtx_options'); if (in_array($post->post_type, array_keys($cvtx_types)) && !($post != null && $post->pinged == '12345')) { $terms = array(); // Update/insert top if ($post->post_type == 'cvtx_top') { // get globally sortable string $_POST['cvtx_sort'] = cvtx_get_sort('cvtx_top', $data = array('top' => isset($_POST['cvtx_top_ord']) ? $_POST['cvtx_top_ord'] : '')); // check whether antraege and applications may be added to this top or not if (!isset($_POST['cvtx_top_antraege'])) { $cvtx_top_antraege = get_post_meta($post_id, 'cvtx_top_antraege', true); $_POST['cvtx_top_antraege'] = is_string($cvtx_top_antraege) && !empty($cvtx_top_antraege) ? $cvtx_top_antraege : 'off'; } if (!isset($_POST['cvtx_top_applications'])) { $cvtx_top_applications = get_post_meta($post_id, 'cvtx_top_applications', true); $_POST['cvtx_top_applications'] = is_string($cvtx_top_applications) && !empty($cvtx_top_applications) ? $cvtx_top_applications : 'off'; } // check whether or not the top is display as appendix if (!isset($_POST['cvtx_top_appendix'])) { $cvtx_top_appendix = get_post_meta($post_id, 'cvtx_top_appendix', true); $_POST['cvtx_top_appendix'] = is_string($cvtx_top_appendix) && !empty($cvtx_top_appendix) ? $cvtx_top_appendix : 'off'; } if (!isset($_POST['cvtx_top_event'])) { $_POST['cvtx_top_event'] = '-1'; } } // update / insert event if ($post->post_type == 'cvtx_event') { // get globally sortable string $sort = 0; $sort += isset($_POST['cvtx_event_year']) ? 10 * intval($_POST['cvtx_event_year']) : 0; $sort += isset($_POST['cvtx_event_ord']) ? roman_to_arabic($_POST['cvtx_event_ord']) : 0; $_POST['cvtx_sort'] = $sort; } else { if ($post->post_type == 'cvtx_antrag' && isset($_POST['cvtx_antrag_top'])) { // get top and validate data $top_ord = get_post_meta($_POST['cvtx_antrag_top'], 'cvtx_top_ord', true); $antrag_ord = isset($_POST['cvtx_antrag_ord']) ? $_POST['cvtx_antrag_ord'] : 0; $event_id = isset($_POST['cvtx_antrag_event']) && is_array($_POST['cvtx_antrag_event']) ? $_POST['cvtx_antrag_event'][0] : (isset($_POST['cvtx_antrag_event']) ? $_POST['cvtx_antrag_event'] : 0); $event_year = get_post_meta($event_id, 'cvtx_event_year', true); $event_nr = get_post_meta($event_id, 'cvtx_event_ord', true); // get globally sortable string $_POST['cvtx_sort'] = cvtx_get_sort('cvtx_antrag', $data = array('top' => $top_ord, 'subject' => $antrag_ord, 'year' => $event_year, 'event' => $event_nr)); if (!isset($_POST['cvtx_antrag_event'])) { $_POST['cvtx_antrag_event'] = '-1'; } // generate short antragsteller if field is empty if (!isset($_POST['cvtx_antrag_steller_short']) || empty($_POST['cvtx_antrag_steller_short'])) { $parts = preg_split('/[,;\\(\\n]+/', $_POST['cvtx_antrag_steller'], 2); if (count($parts) == 2) { $_POST['cvtx_antrag_steller_short'] = trim($parts[0]) . ' ' . __('et al.', 'cvtx'); } else { $_POST['cvtx_antrag_steller_short'] = $_POST['cvtx_antrag_steller']; } } // get default reader terms for amendments $terms = isset($options['cvtx_default_reader_antrag']) ? $options['cvtx_default_reader_antrag'] : array(); } else { if ($post->post_type == 'cvtx_aeantrag' && isset($_POST['cvtx_aeantrag_antrag']) && isset($_POST['cvtx_aeantrag_zeile'])) { // get top and antrag and validate data $top_id = get_post_meta($_POST['cvtx_aeantrag_antrag'], 'cvtx_antrag_top', true); $top_ord = get_post_meta($top_id, 'cvtx_top_ord', true); $antrag_ord = get_post_meta($_POST['cvtx_aeantrag_antrag'], 'cvtx_antrag_ord', true); $event_id = get_post_meta($_POST['cvtx_aeantrag_antrag'], 'cvtx_antrag_event', false); $event_id = is_array($event_id) ? array_pop($event_id) : $event_id; $event_year = get_post_meta($event_id, 'cvtx_event_year', true); $event_nr = get_post_meta($event_id, 'cvtx_event_ord', true); // fetch main info on line (first few numbers) if (preg_match('/^([0-9]+)/', $_POST['cvtx_aeantrag_zeile'], $match1)) { $aeantrag_zeile = $match1[1]; } else { $aeantrag_zeile = 0; } // look for vari-ending (some kind of -2 at the end of the line field) if (preg_match('/([0-9]+)$/', $_POST['cvtx_aeantrag_zeile'], $match2) && strlen($match2[1]) < strlen($_POST['cvtx_aeantrag_zeile'])) { $aeantrag_vari = $match2[1]; } else { $aeantrag_vari = false; } // get globally sortable string $_POST['cvtx_sort'] = cvtx_get_sort('cvtx_aeantrag', $data = array('top' => $top_ord, 'subject' => $antrag_ord, 'zeile' => $aeantrag_zeile, 'vari' => $aeantrag_vari, 'year' => $event_year, 'event' => $event_nr)); // generate short antragsteller if field is empty if (!isset($_POST['cvtx_aeantrag_steller_short']) || empty($_POST['cvtx_aeantrag_steller_short'])) { $parts = preg_split('/[,;\\(\\n]+/', $_POST['cvtx_aeantrag_steller'], 2); if (count($parts) == 2) { $_POST['cvtx_aeantrag_steller_short'] = trim($parts[0]) . ' ' . __('et al.', 'cvtx'); } else { $_POST['cvtx_aeantrag_steller_short'] = $_POST['cvtx_aeantrag_steller']; } } // get default reader terms for amendments $terms = $options['cvtx_default_reader_aeantrag']; } else { if ($post->post_type == 'cvtx_reader') { // Add term if new reader is created if (!term_exists('cvtx_reader_' . $post_id, 'cvtx_tax_reader')) { wp_insert_term('cvtx_reader_' . $post_id, 'cvtx_tax_reader'); } // get all previously selected posts for this reader term $old = array(); $query = new WP_Query(array('post_type' => array('cvtx_antrag', 'cvtx_aeantrag', 'cvtx_application'), 'taxonomy' => 'cvtx_tax_reader', 'term' => 'cvtx_reader_' . intval($post_id), 'orderby' => 'meta_value', 'meta_key' => 'cvtx_sort', 'order' => 'ASC', 'nopaging' => true)); while ($query->have_posts()) { $query->the_post(); $old[] = get_the_ID(); } // get all selected posts for this reader term $new = array(); if (isset($_POST['cvtx_post_ids']) && is_array($_POST['cvtx_post_ids'])) { $new = array_keys($_POST['cvtx_post_ids']); } // fetch object terms for added, unselected and remaining objects and copy all of them except this reader! $terms = array(); foreach (array_unique(array_merge($old, $new)) as $item) { $terms["{$item}"] = array(); foreach (wp_get_object_terms($item, 'cvtx_tax_reader') as $term) { if ($term->name != 'cvtx_reader_' . intval($post_id)) { $terms["{$item}"][] = $term->name; } } } // update object terms of unselected objects foreach ($old as $item) { if (!in_array($item, $new)) { wp_set_object_terms($item, $terms["{$item}"], 'cvtx_tax_reader'); } } // add this reader to terms list and update object terms of newly added objects foreach ($new as $item) { $terms["{$item}"][] = 'cvtx_reader_' . intval($post_id); if (!in_array($item, $old)) { wp_set_object_terms($item, $terms["{$item}"], 'cvtx_tax_reader'); } } $out_dir = wp_upload_dir(); $old_file = cvtx_get_file($post, 'reader_titlepage', 'dir'); $filename = $out_dir['path'] . '/' . $post->ID . '_titlepage.pdf'; $attach_titlepage = array('post_mime_type' => 'application/pdf', 'post_title' => $post->post_type . '_titlepage_' . $post->ID, 'post_content' => '', 'post_status' => 'inherit', 'post_parent' => $post->ID); if (isset($_FILES['cvtx_reader_titlepage']) && $_FILES['cvtx_reader_titlepage']['size'] > 0) { $file = $_FILES['cvtx_reader_titlepage']; // of which filetype is the uploaded file? $validate = wp_check_filetype_and_ext($file['tmp_name'], basename($file['name'])); // we accept only pdfs! if (in_array($validate['type'], array('application/pdf'))) { // is there already an attachment? remove it if ($existing = get_post_meta($post->ID, 'cvtx_reader_titlepage_id', true)) { wp_delete_attachment($existing, true); } // upload the image $upload = wp_handle_upload($file, array('test_form' => false)); // check if upload was succesful, get meta-informations if (!isset($upload['error']) && isset($upload['file'])) { // move file rename($upload['file'], $filename); // insert attachment $attach_id = wp_insert_attachment($attach_titlepage, $filename); $attach_data = wp_generate_attachment_metadata($attach_id, $filename); wp_update_attachment_metadata($attach_id, $attach_data); // save attachment id to application update_post_meta($post->ID, 'cvtx_reader_titlepage_id', $attach_id); } } else { //cvtx_error_message('Please use one of the following image types: '.implode(', ',$cvtx_allowed_image_types)); } } else { if ($old_file !== false && $old_file != $filename) { // move file rename($old_file, $filename); // delete old attachment if ($existing = get_post_meta($post->ID, 'cvtx_reader_titlepage_id', true)) { wp_delete_attachment($existing, true); } // insert attachment $attach_id = wp_insert_attachment($attach_titlepage, $filename); $attach_data = wp_generate_attachment_metadata($attach_id, $filename); wp_update_attachment_metadata($attach_id, $attach_data); // save attachment id to application update_post_meta($post->ID, 'cvtx_reader_titlepage_id', $attach_id); } } } else { if ($post->post_type == 'cvtx_application') { // get top and validate data $top_ord = get_post_meta($_POST['cvtx_application_top'], 'cvtx_top_ord', true); $appl_ord = isset($_POST['cvtx_application_ord']) ? $_POST['cvtx_application_ord'] : 0; // get globally sortable string $_POST['cvtx_sort'] = cvtx_get_sort('cvtx_application', $data = array('top' => $top_ord, 'subject' => $appl_ord)); // check whether or not the email will be published if (!isset($_POST['cvtx_application_email_public']) || $_POST['cvtx_application_email_public'] == false) { $_POST['cvtx_application_email_public'] = 'off'; } if (!isset($_POST['cvtx_application_event'])) { $_POST['cvtx_application_event'] = '-1'; } // check whether manually file upload enabled if (!isset($_POST['cvtx_application_manually'])) { $_POST['cvtx_application_manually'] = 'off'; } // get default reader terms for applications if (isset($options['cvtx_default_reader_application'])) { $terms = $options['cvtx_default_reader_application']; } else { $terms = false; } /* DAS IST NOCH IMMER EHER QUICK UND DIRTY */ if ($post->post_status != 'auto-draft') { // get old filenames $old_file = cvtx_get_file($post, 'pdf', 'dir'); $old_photo = cvtx_get_file($post, 'application_photo', 'dir'); // generate file name $out_dir = wp_upload_dir(); // generate short (BUGGY!!!) $top = get_post_meta($_POST['cvtx_application_top'], 'cvtx_top_short', true); // format $format = strtr($options['cvtx_antrag_format'], array(__('%agenda_point%', 'cvtx') => $top, __('%resolution%', 'cvtx') => $appl_ord)); if (!empty($top) && !empty($_POST['cvtx_application_ord'])) { $short = $format; } // application published? if ($post->post_status == 'publish' && isset($short)) { $filename = $out_dir['path'] . '/' . cvtx_sanitize_file_name($short . '_' . $post->post_title) . '.pdf'; } else { $filename = $out_dir['path'] . '/' . $post->ID . '.pdf'; } $filename_photo = $out_dir['path'] . '/' . $post->ID . '_photo.jpg'; // configure attachment $attachment = array('post_mime_type' => 'application/pdf', 'post_title' => $post->post_type . '_' . $post->ID, 'post_content' => '', 'post_status' => 'inherit', 'post_parent' => $post->ID); $attach_photo = array('post_mime_type' => 'image/jpeg', 'post_title' => $post->post_type . '_photo_' . $post->ID, 'post_content' => '', 'post_status' => 'inherit', 'post_parent' => $post->ID); // file upload if (isset($_FILES['cvtx_application_file']) && $_FILES['cvtx_application_file']['size'] > 0) { $file = $_FILES['cvtx_application_file']; // of which filetype is the uploaded file? $arr_filetype = wp_check_filetype(basename($file['name'])); $filetype = $arr_filetype['type']; // we accept only pdfs! if ($filetype == 'application/pdf') { // is there already an attachment? remove it if ($existing = get_post_meta($post->ID, 'cvtx_pdf_id', true)) { wp_delete_attachment($existing, true); } // upload the pdf $upload = wp_handle_upload($file, array('test_form' => false)); // check if upload was succesful, get meta-informations if (!isset($upload['error']) && isset($upload['file'])) { // move file rename($upload['file'], $filename); // insert attachment $attach_id = wp_insert_attachment($attachment, $filename); // save attachment id to application update_post_meta($post->ID, 'cvtx_pdf_id', $attach_id); } } } else { if ($old_file !== false && $old_file != $filename) { // move file rename($old_file, $filename); // delete old attachment if ($existing = get_post_meta($post->ID, 'cvtx_pdf_id', true)) { wp_delete_attachment($existing, true); } // insert attachment $attach_id = wp_insert_attachment($attachment, $filename); // save attachment id to application update_post_meta($post->ID, 'cvtx_pdf_id', $attach_id); } } // photo upload $max_image_size = $options['cvtx_max_image_size']; if (isset($_FILES['cvtx_application_photo']) && $_FILES['cvtx_application_photo']['size'] > 0 && $_FILES['cvtx_application_photo']['size'] < $max_image_size * 1000) { $file = $_FILES['cvtx_application_photo']; // of which filetype is the uploaded file? $validate = wp_check_filetype_and_ext($file['tmp_name'], basename($file['name'])); // we accept only images! if (in_array($validate['type'], $cvtx_allowed_image_types)) { // is there already an attachment? remove it if ($existing = get_post_meta($post->ID, 'cvtx_application_photo_id', true)) { wp_delete_attachment($existing, true); } // upload the image $upload = wp_handle_upload($file, array('test_form' => false)); // check if upload was succesful, get meta-informations if (!isset($upload['error']) && isset($upload['file'])) { // move file rename($upload['file'], $filename_photo); // insert attachment $attach_id = wp_insert_attachment($attach_photo, $filename_photo); $attach_data = wp_generate_attachment_metadata($attach_id, $filename_photo); wp_update_attachment_metadata($attach_id, $attach_data); // save attachment id to application update_post_meta($post->ID, 'cvtx_application_photo_id', $attach_id); } } else { //cvtx_error_message('Please use one of the following image types: '.implode(', ',$cvtx_allowed_image_types)); } } else { if ($_FILES['cvtx_application_photo']['size'] > $max_image_size * 1000) { // message too large } else { if ($old_photo !== false && $old_photo != $filename_photo) { // move file rename($old_photo, $filename_photo); // delete old attachment if ($existing = get_post_meta($post->ID, 'cvtx_application_photo_id', true)) { wp_delete_attachment($existing, true); } // insert attachment $attach_id = wp_insert_attachment($attach_photo, $filename_photo); $attach_data = wp_generate_attachment_metadata($attach_id, $filename_photo); wp_update_attachment_metadata($attach_id, $attach_data); // save attachment id to application update_post_meta($post->ID, 'cvtx_application_photo_id', $attach_id); } } } } } } } } } // add default reader terms to antrag, amendment or application if (is_array($terms) && count($terms) > 0 && $post->post_type != 'cvtx_reader') { $items = array(); foreach ($terms as $term) { if (term_exists($term, 'cvtx_tax_reader')) { $items[] = $term; } } if (count($items) > 0) { wp_set_object_terms($post->ID, $items, 'cvtx_tax_reader'); } } // Loop through the POST data foreach ($cvtx_types[$post->post_type] as $key) { if (isset($_POST[$key])) { // save data $value = @$_POST[$key]; if (empty($value)) { delete_post_meta($post_id, $key); continue; } // If value is a string it should be unique if (!is_array($value)) { // Update meta if (!update_post_meta($post_id, $key, $value)) { // Or add the meta data add_post_meta($post_id, $key, $value); } } else { // If passed along is an array, we should remove all previous data delete_post_meta($post_id, $key); // Loop through the array adding new values to the post meta as different entries with the same name foreach ($value as $entry) { add_post_meta($post_id, $key, $entry); } } } } // create pdf if (is_admin() & !(isset($_POST['cvtx_application_manually']) && $_POST['cvtx_application_manually'] == 'on')) { cvtx_create_pdf($post_id, $post); } else { $tpl = get_template_directory() . '/mail.php'; if ($options['cvtx_send_html_mail'] == FALSE || !file_exists($tpl)) { $html_mail = FALSE; } else { $html_mail = TRUE; } $headers = array('From: ' . (!empty($options['cvtx_send_from_email']) ? $options['cvtx_send_from_email'] : get_bloginfo('admin_email')) . "\r\n", $html_mail ? "Content-Type: text/html\r\n" : ''); // post type antrag created if ($post->post_type == 'cvtx_antrag') { $mails['owner'] = array('subject' => $options['cvtx_send_create_antrag_owner_subject'], 'body' => $options['cvtx_send_create_antrag_owner_body']); $mails['admin'] = array('subject' => $options['cvtx_send_create_antrag_admin_subject'], 'body' => $options['cvtx_send_create_antrag_admin_body']); $fields = array(__('%agenda_point_token%', 'cvtx') => __('Agenda point', 'cvtx') . ' ' . get_post_meta($_POST['cvtx_antrag_top'], 'cvtx_top_ord', true), __('%agenda_point%', 'cvtx') => get_the_title($_POST['cvtx_antrag_top']), __('%title%', 'cvtx') => $post->post_title, __('%authors%', 'cvtx') => $_POST['cvtx_antrag_steller'], __('%authors_short%', 'cvtx') => $_POST['cvtx_antrag_steller_short'], __('%text%', 'cvtx') => $post->post_content, __('%explanation%', 'cvtx') => $_POST['cvtx_antrag_grund']); // replace post type data foreach ($mails as $rcpt => $mail) { foreach ($mail as $part => $content) { if ($part == 'body' && $html_mail) { $content = nl2br(strtr($content, $fields)); ob_start(); require $tpl; $out = ob_get_contents(); ob_end_clean(); $mails[$rcpt][$part] = $out; } else { $mails[$rcpt][$part] = strtr($content, $fields); } } } // send mail(s) if option enabled if ($options['cvtx_send_create_antrag_owner']) { wp_mail($_POST['cvtx_antrag_email'], $mails['owner']['subject'], $mails['owner']['body'], implode("\r\n", $headers) . "\r\n"); } if ($options['cvtx_send_create_antrag_admin']) { wp_mail(isset($options['cvtx_send_rcpt_email']) ? $options['cvtx_send_rcpt_email'] : get_bloginfo('admin_email'), $mails['admin']['subject'], $mails['admin']['body'], implode("\r\n", $headers)); } } else { if ($post->post_type == 'cvtx_aeantrag') { $mails['owner'] = array('subject' => $options['cvtx_send_create_aeantrag_owner_subject'], 'body' => $options['cvtx_send_create_aeantrag_owner_body']); $mails['admin'] = array('subject' => $options['cvtx_send_create_aeantrag_admin_subject'], 'body' => $options['cvtx_send_create_aeantrag_admin_body']); $top_id = get_post_meta($_POST['cvtx_aeantrag_antrag'], 'cvtx_antrag_top', true); $fields = array(__('%agenda_point_token%', 'cvtx') => __('Agenda point', 'cvtx') . ' ' . get_post_meta($top_id, 'cvtx_top_ord', true), __('%agenda_point%', 'cvtx') => get_the_title($top_id), __('%resolution_token%', 'cvtx') => get_post_meta($top_id, 'cvtx_top_short', true) . '-' . get_post_meta($_POST['cvtx_aeantrag_antrag'], 'cvtx_antrag_ord', true), __('%resolution%', 'cvtx') => get_the_title($_POST['cvtx_aeantrag_antrag']), __('%line%', 'cvtx') => $_POST['cvtx_aeantrag_zeile'], __('%authors%', 'cvtx') => $_POST['cvtx_aeantrag_steller'], __('%authors_short%', 'cvtx') => $_POST['cvtx_aeantrag_steller_short'], __('%text%', 'cvtx') => $post->post_content, __('%explanation%', 'cvtx') => $_POST['cvtx_aeantrag_grund']); // replace post type data foreach ($mails as $rcpt => $mail) { foreach ($mail as $part => $content) { if ($part == 'body' && $html_mail) { $content = nl2br(strtr($content, $fields)); ob_start(); require $tpl; $out = ob_get_contents(); ob_end_clean(); $mails[$rcpt][$part] = $out; } else { $mails[$rcpt][$part] = strtr($content, $fields); } } } // send mail(s) if option enabled if ($options['cvtx_send_create_aeantrag_owner']) { wp_mail($_POST['cvtx_aeantrag_email'], $mails['owner']['subject'], $mails['owner']['body'], $headers); } if ($options['cvtx_send_create_aeantrag_admin']) { wp_mail($options['cvtx_send_rcpt_email'], $mails['admin']['subject'], $mails['admin']['body'], $headers); } } } } } }