add_action('admin_notices', 'CCTM::print_notices'); if (empty(CCTM::$errors)) { // Load up the CCTM data from wp_options, populates CCTM::$data CCTM::load_data(); // Shortcodes add_shortcode('summarize-posts', 'SummarizePosts::get_posts'); add_shortcode('summarize_posts', 'SummarizePosts::get_posts'); add_shortcode('custom_field', 'CCTM::custom_field'); add_shortcode('cctm_post_form', 'CCTM::cctm_post_form'); // Summarize Posts Tiny MCE button if (CCTM::get_setting('summarizeposts_tinymce')) { add_filter('mce_external_plugins', 'SummarizePosts::tinyplugin_register'); add_filter('mce_buttons', 'SummarizePosts::tinyplugin_add_button', 0); } // Custom Fields Tiny MCE button if (CCTM::get_setting('custom_fields_tinymce')) { add_filter('mce_external_plugins', 'CCTM::tinyplugin_register'); add_filter('mce_buttons', 'CCTM::tinyplugin_add_button', 0); } // Run any updates for this version. add_action('init', 'CCTM::check_for_updates', 0); // Register any custom post-types (a.k.a. content types) add_action('init', 'CCTM::register_custom_post_types', 11); add_action('widgets_init', 'SummarizePosts_Widget::register_this_widget'); add_action('widgets_init', 'CCTM_Post_Widget::register_this_widget'); if (is_admin()) { // Generate admin menu, bootstrap CSS/JS add_action('admin_init', 'CCTM::admin_init'); // Create custom plugin settings menu add_action('admin_menu', 'CCTM::create_admin_menu'); add_filter('plugin_action_links', 'CCTM::add_plugin_settings_link', 10, 2);
/** * Save the new Custom Fields values. If the content type is not active in the * CCTM plugin or its custom fields are not being standardized, then this function * effectively does nothing. * * WARNING: This function is also called when the wp_insert_post() is called, and * we don't want to step on its toes. We want this to kick in ONLY when a post * is inserted via the WP manager. * see http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=52 * * @param integer $post_id id of the post these custom fields are associated with * @param object $post the post object */ public static function save_custom_fields($post_id, $post) { // Bail if you're not in the admin editing a post if (!self::_is_existing_post() && !self::_is_new_post()) { return; } // Bail if this post-type is not active in the CCTM if (!isset(CCTM::$data['post_type_defs'][$post->post_type]['is_active']) || CCTM::$data['post_type_defs'][$post->post_type]['is_active'] == 0) { return; } // Bail if there are no custom fields defined in the CCTM if (empty(CCTM::$data['post_type_defs'][$post->post_type]['custom_fields'])) { return; } // See issue http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=80 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } // Use this to ensure you save custom fields only when saving from the edit/create post page $nonce = CCTM::get_value($_POST, '_cctm_nonce'); if (!wp_verify_nonce($nonce, 'cctm_create_update_post')) { return; } if (!empty($_POST)) { $custom_fields = self::_get_custom_fields($post->post_type); $validation_errors = array(); foreach ($custom_fields as $field_name) { if (!isset(CCTM::$data['custom_field_defs'][$field_name]['type'])) { continue; } $field_type = CCTM::$data['custom_field_defs'][$field_name]['type']; if ($FieldObj = CCTM::load_object($field_type, 'fields')) { $FieldObj->set_props(CCTM::$data['custom_field_defs'][$field_name]); $value = $FieldObj->save_post_filter($_POST, $field_name); CCTM::log("Saving field Type: {$field_type} with value: {$value}", __FILE__, __LINE__); // Custom fields can return a literal null if they don't save data to the db. if ($value !== null) { // Check for empty json arrays, e.g. [""], convert them to empty PHP array() $value_copy = $value; if ($FieldObj->is_repeatable) { $value_copy = json_decode(stripslashes($value), true); if (is_array($value_copy)) { foreach ($value_copy as $k => $v) { if (empty($v)) { unset($value_copy[$k]); } } } } // We do some more work to ensure the database stays lean if (is_array($value_copy) && empty($value_copy) && !CCTM::get_setting('save_empty_fields')) { delete_post_meta($post_id, $field_name); } if (!is_array($value_copy) && !strlen(trim($value_copy)) && !CCTM::get_setting('save_empty_fields')) { // Delete the row from wp_postmeta, or don't write it at all delete_post_meta($post_id, $field_name); } else { update_post_meta($post_id, $field_name, $value); } } } else { // error! Can't include the field class. WTF did you do? } } // Pass validation errors like this: fieldname => validator, e.g. myfield => required if (!empty($validation_errors)) { CCTM::log('Validation errors: ' . json_encode($validation_errors), __FILE__, __LINE__); CCTM::set_flash(json_encode($validation_errors)); } } }