public function submit() { global $wpdb; $action = MasterPress::$action; if ($action == "edit") { $template = new MPM_Template(); if ($_POST["supports_type"] == "inherit") { $template->supports = "*"; } else { $template->supports = implode(",", $_POST["supports"]); } $template->visibility = $this->get_visibility_val("post_types"); $template->update(MasterPress::$id); return $template; } else if ($action == "create-field-set" || $action == "edit-field-set") { $field_set = new MPM_TemplateFieldSet(); // consume the post data $field_set->name = $_POST["name"]; $field_set->singular_name = $_POST["singular_name"]; $field_set->disabled = isset($_POST["disabled"]); $field_set->labels = $_POST["labels"]; $field_set->capabilities = self::handle_capabilities(); $field_set->visibility = $_POST["visibility"]; $field_set->allow_multiple = isset($_POST["allow_multiple"]); $field_set->type = "t"; // t = template $field_set->icon = self::handle_icon("icon", "icon_select"); $field_set->position = $_POST["position"]; $field_set->expanded = isset($_POST["expanded"]); $field_set->sidebar = isset($_POST["sidebar"]); $field_set->versions = $_POST["versions"]; $field_set->visibility = $this->get_visibility_val(); if (MPC::is_create()) { $field_set->insert(); } else if (MPC::is_edit()) { $field_set->update(MasterPress::$id); } if ($field_set->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_set_meta($field_set, $_POST["name_original"]); } // we don't attach post types to these. they are implicitly linked to the built-in "page" post type // update other menu positions $op = $_POST["other_position"]; if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("field-sets"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } return $field_set; } else if ($action == "delete-field-set") { $fg = MPM_FieldSet::find_by_id(MasterPress::$id); if ($fg) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $fg->delete_meta(); } } $fg->delete(); return true; } else if ($action == "delete-field") { $f = MPM_Field::find_by_id(MasterPress::$id); if ($f) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $f->delete_meta(); } } $f->delete(); return true; } else if ($action == "create-field" || $action == "edit-field") { // FIELD OPERATIONS (NOT FIELD GROUPS!) $field = new MPM_Field(); // consume the post data $field->field_set_id = $_POST["parent"]; $field->name = $_POST["name"]; $field->disabled = isset($_POST["disabled"]); $field->required = isset($_POST["required"]); $field->summary_options = $_POST["summary_options"]; $field->labels = $_POST["labels"]; $field->icon = self::handle_icon("icon", "icon_select"); $field->type = $_POST["type"]; $field->type_options = $_POST["type_options"]; $field->position = $_POST["position"]; $field->visibility = $this->get_visibility_val(); $field->capabilities = self::handle_capabilities(); if (MPC::is_create()) { $field->insert(); } else if (MPC::is_edit()) { $field->update(MasterPress::$id); } if ($field->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_meta($field, $_POST["name_original"]); } // update other menu positions $op = $_POST["other_position"]; if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("fields"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } return $field; } return false; }
public static function do_download_file($type) { global $wf; // used by the "From URL" control if (isset($_REQUEST["url"])) { $url = trim($_REQUEST["url"]); if ($url) { MPC::incl("files"); $model_id = $_REQUEST["model_id"]; // need to check the extensions $pi = pathinfo(urldecode($url)); $field = MPM_Field::find_by_id($model_id); if ($field) { $type_options = $field->type_options; $extensions = $type_options["allowed_types"]; if (!in_array(strtolower($pi["extension"]), $extensions)) { self::ajax_error( sprintf( __("Cannot download %s. This field only allows the file types %s", MASTERPRESS_DOMAIN ), $type, implode(", ", $extensions))); } list($dir, $sub) = MPC_Files::upload_dir($field); $name = MPC_Files::sanitize_filename($pi["filename"], $type_options).".".md5($url); if ($type == "image") { $file = $wf->image_from_url($url, $name, $dir); } else { $file = $wf->file_from_url($url, $name, $dir); } if ($file->exists()) { // check the file size $limit = self::get_filesize_limit(); if (isset($type_options["allowed_maxsize"])) { if (is_numeric($type_options["allowed_maxsize"])) { $limit = WOOF_File::to_bytes($type_options["allowed_maxsize"]."M"); } } if ($file->filesizeinbytes() > $limit) { $file->delete(); self::ajax_error( sprintf( __("The %s was downloaded, but it could not saved as it was too large. This field only allows files up to %s", MASTERPRESS_DOMAIN ), $type, WOOF_File::format_filesize($limit, "MB", TRUE, $sep = " "))); } $info = array( "url" => $file->permalink() ); self::ajax_success($info); } else { self::ajax_error( sprintf( __("The %s could not be downloaded. Please check the URL is valid and try again", MASTERPRESS_DOMAIN ), $type ) ); } } else { self::ajax_error( sprintf( __( "This %s field could not be found in the database to check the validity of this download.", MASTERPRESS_DOMAIN ), $type ) ); } } } self::ajax_error(__("No URL specified", MASTERPRESS_DOMAIN)); }
public function submit() { global $wpdb; $action = MasterPress::$action; if ($action == "create" || $action == "edit") { $builtin = $_POST["_builtin"] == "true"; $external = $_POST["_external"] == "true"; if ($builtin) { $tax = MPM_Taxonomy::find_by_name($_POST["name"]); $tax->disabled = isset($_POST["disabled"]); $tax->object_type = $_POST["post_types"]; $tax->visibility = $this->get_visibility_val("sites"); $tax->title_icon = self::handle_icon("title_icon", "title_icon_select"); $tax->columns = $_POST["columns"]; if ($action == "edit") { $tax->update(MasterPress::$id); } } else if ($external) { $tax = MPM_Taxonomy::find_by_name($_POST["name"]); $tax->columns = $_POST["columns"]; $tax->title_icon = self::handle_icon("title_icon", "title_icon_select"); if ($action == "edit") { $tax->update(MasterPress::$id); } } else { $tax = new MPM_Taxonomy(); // consume the post data $tax->name = $_POST["name"]; $tax->plural_name = $_POST["plural_name"]; $tax->object_type = MPC::post_val("post_types"); $tax->disabled = isset($_POST["disabled"]); $tax->labels = $_POST["labels"]; $tax->show_ui = isset($_POST["show_ui"]); $tax->hide_term_ui = isset($_POST["hide_term_ui"]); $tax->show_in_nav_menus = isset($_POST["show_in_nav_menus"]); $tax->show_manage_filter = isset($_POST["show_manage_filter"]); $tax->show_tagcloud = isset($_POST["show_tagcloud"]); $tax->hierarchical = isset($_POST["hierarchical"]); $tax->query_var = $_POST["query_var"]; $tax->title_icon = self::handle_icon("title_icon", "title_icon_select"); $tax->capabilities = $_POST["capabilities"]; $tax->update_count_callback = $_POST["update_count_callback"]; $tax->visibility = $this->get_visibility_val("sites,taxonomies"); $tax->columns = $_POST["columns"]; $rewrite = array( "slug" => $_POST["rewrite"]["slug"], "with_front" => isset($_POST["rewrite"]["with_front"]), "hierarchical" => isset($_POST["rewrite"]["hierarchical"]) ); $tax->rewrite = $rewrite; $tax->_builtin = false; $tax->_external = $external; if ($action == "create") { $tax->insert(); } else if ($action == "edit") { $tax->update(MasterPress::$id); if ($tax->is_valid()) { global $meow_provider; $meow_provider->migrate_taxonomy($tax, $_POST["name_original"]); } } } if ($tax->is_valid()) { MasterPress::flag_for_rewrite(); } return $tax; } else if ($action == "delete") { $tax = MPM_Taxonomy::find_by_id(MasterPress::$id); if ($tax) { $tax->delete( array( "existing_terms" => MPC::post_val("existing_terms"), "existing_terms_reassign_taxonomy" => MPC::post_val("existing_terms_reassign_taxonomy"), "field_sets" => MPC::post_val("field_sets"), "field_data" => MPC::post_val("field_data") ) ); } MasterPress::flag_for_rewrite(); return true; } else if ($action == "create-field-set" || $action == "edit-field-set") { $field_set = new MPM_TaxonomyFieldSet(); // consume the post data $field_set->name = $_POST["name"]; $field_set->singular_name = $_POST["singular_name"]; $field_set->disabled = isset($_POST["disabled"]); $field_set->labels = $_POST["labels"]; $field_set->allow_multiple = isset($_POST["allow_multiple"]); $field_set->visibility = $_POST["visibility"]; $field_set->capabilities = self::handle_capabilities(); $field_set->icon = self::handle_icon("icon", "icon_select"); $field_set->type = "x"; // x = taXonomy $field_set->position = $_POST["position"]; $field_set->expanded = isset($_POST["expanded"]); $field_set->sidebar = isset($_POST["sidebar"]); $field_set->versions = $_POST["versions"]; $field_set->visibility = $this->get_visibility_val("sites,taxonomies"); if (MPC::is_create()) { $field_set->insert(); } else if (MPC::is_edit()) { $field_set->update(MasterPress::$id); } if ($field_set->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_set_meta($field_set, $_POST["name_original"]); } // update other menu positions $op = $_POST["other_position"]; if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("field-sets"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } return $field_set; } else if ($action == "delete-field-set") { $fg = MPM_TaxonomyFieldSet::find_by_id(MasterPress::$id); if ($fg) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $fg->delete_meta(); } } $fg->delete(); return true; } else if ($action == "delete-field") { $f = MPM_Field::find_by_id(MasterPress::$id); if ($f) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $f->delete_meta(); } } $f->delete(); return true; } else if ($action == "create-field" || $action == "edit-field") { // FIELD OPERATIONS (NOT FIELD GROUPS!) $field = new MPM_Field(); // consume the post data $field->field_set_id = $_POST["parent"]; $field->name = $_POST["name"]; $field->disabled = isset($_POST["disabled"]); $field->summary_options = $_POST["summary_options"]; $field->required = isset($_POST["required"]); $field->labels = $_POST["labels"]; $field->icon = self::handle_icon("icon", "icon_select"); $field->type = $_POST["type"]; $field->type_options = $_POST["type_options"]; $field->position = $_POST["position"]; $field->visibility = $this->get_visibility_val("sites,taxonomies"); $field->capabilities = self::handle_capabilities(); $fg = MPM_TaxonomyFieldSet::find_by_id($_POST["parent"]); if (MPC::is_create()) { $field->insert(); } else if (MPC::is_edit()) { $field->update(MasterPress::$id); } if ($field->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_meta($field, $_POST["name_original"]); } // update other menu positions $op = $_POST["other_position"]; if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("fields"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } return $field; } }
public static function refresh() { global $wf; $values = json_decode(stripslashes($_REQUEST["values"]), true); if (!is_array($values)) { $values = array($values); } $model_id = $_REQUEST["model_id"]; $field = MPM_Field::find_by_id($model_id); if ($field) { $selects = self::selects($field->type_options, $values); $info["select"] = WOOF::render_template($selects["select"], array("id" => $_REQUEST["id"], "name" => str_replace("[]", "", $_REQUEST["name"]) ) ); self::ajax_success( $info ); } }
public function options() { // construct the HTML for the options UI $id = ""; $type = ""; if (isset($_REQUEST["type"])) { $type = $_REQUEST["type"]; } if (isset($_GET["id"])) { $id = $_GET["id"]; } // if the id is present, load a model if ($id && $id != "") { MPM::incl("field"); $model = MPM_Field::find_by_id($id); } if ($type != "") { if ($type_class = MPFT::type_class($type)) { $type_options = array(); if (isset($model) && isset($model->type_options)) { $type_options = $model->type_options; } $form = call_user_func_array( array($type_class, "options_form"), array(MPM::array_stripslashes($type_options)) ); } $css_url = ""; $css_file = MPU::type_file_path($type, "mpft-$type.options.css"); if (file_exists($css_file)) { $css_url = MPU::type_file_url($type, "mpft-$type.options.css")."?".filemtime($css_file); } $js_file = MPU::type_file_path($type, "mpft-$type.options.js"); if (file_exists($js_file)) { $js_url = MPU::type_file_url($type, "mpft-$type.options.js")."?".filemtime($js_file); } $ret = array( "form" => $form, "css_file" => $css_url, "type" => $type ); if (isset($js_url)) { $ret["js_file"] = $js_url; } self::ajax_success( $ret ); } else { self::ajax_error( __("field type options could not be loaded", MASTERPRESS_DOMAIN) ); } }
public function submit() { global $wpdb, $wf; $action = MasterPress::$action; if ($action == "create") { $role_id = $_POST["name"]; if ($role_id == "") { MPV::err(__("A role name must be provided", MASTERPRESS_DOMAIN) ); return false; } // check that there isn't a role of this name already $role = $wf->role($role_id); if ($role && !is_woof_silent($role)) { MPV::err( sprintf( __("Sorry a role named %s already exists. Please choose another name", MASTERPRESS_DOMAIN), $role_id ) ); return false; } // all okay, save the role if (isset($_POST["display_name"]) && trim($_POST["display_name"]) != "") { $display_name = $_POST["display_name"]; } else { $display_name = WOOF_Inflector::titleize($role_id); } $caps = array_keys($_POST["cap"]); foreach ($_POST["new_caps"] as $cap) { if (trim($cap) != "") { $norm = WOOF_Inflector::underscore($cap); if (!in_array($norm, $_POST["all_caps"])) { $caps[] = $norm; } } } add_role($role_id, $display_name, array_fill_keys($caps, true) ); return true; } else if ($action == "edit") { $role_id = MasterPress::$id; $wf_role = $wf->role($role_id); if (!is_woof_silent($wf_role)) { $role = get_role($wf_role->id()); $all_caps = explode(",", $_POST["all_caps"]); $selected_caps = array_keys($_POST["cap"]); $role_caps = array_keys($role->capabilities); $add_caps = array_diff($selected_caps, $role_caps); $remove_caps = array_diff($all_caps, $selected_caps); foreach ($remove_caps as $cap) { if ($role->has_cap($cap)) { $role->remove_cap($cap); } } foreach ($_POST["new_caps"] as $cap) { if (trim($cap) != "") { $norm = WOOF_Inflector::underscore($cap); if (!in_array($norm, $all_caps)) { $add_caps[] = $norm; } } } foreach ($add_caps as $cap) { if (!$role->has_cap($cap)) { $role->add_cap($cap); } } } return true; } else if ($action == "create-field-set" || $action == "edit-field-set") { $field_set = new MPM_RoleFieldSet(); // consume the post data $field_set->name = $_POST["name"]; $field_set->singular_name = $_POST["singular_name"]; $field_set->disabled = isset($_POST["disabled"]); $field_set->labels = $_POST["labels"]; $field_set->visibility = $_POST["visibility"]; $field_set->capabilities = self::handle_capabilities(); $field_set->allow_multiple = isset($_POST["allow_multiple"]); $field_set->type = "r"; // r = role $field_set->icon = self::handle_icon("icon", "icon_select"); $field_set->position = $_POST["position"]; $field_set->expanded = isset($_POST["expanded"]); $field_set->sidebar = false; $field_set->visibility = $this->get_visibility_val("sites,roles"); if (MPC::is_create()) { $field_set->insert(); } else if (MPC::is_edit()) { $field_set->update(MasterPress::$id); } if ($field_set->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_set_meta($field_set, $_POST["name_original"]); } // we don't attach post types to these. they are implicitly linked to the built-in "page" post type // update other menu positions $op = $_POST["other_position"]; if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("field-sets"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } return $field_set; } else if ($action == "delete-field-set") { $fg = MPM_FieldSet::find_by_id(MasterPress::$id); if ($fg) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $fg->delete_meta(); } } $fg->delete(); return true; } else if ($action == "delete-field") { $f = MPM_Field::find_by_id(MasterPress::$id); if ($f) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $f->delete_meta(); } } $f->delete(); return true; } else if ($action == "create-field" || $action == "edit-field") { // FIELD OPERATIONS (NOT FIELD GROUPS!) $field = new MPM_Field(); // consume the post data $field->field_set_id = $_POST["parent"]; $field->name = $_POST["name"]; $field->disabled = isset($_POST["disabled"]); $field->required = isset($_POST["required"]); $field->summary_options = $_POST["summary_options"]; $field->labels = $_POST["labels"]; $field->icon = self::handle_icon("icon", "icon_select"); $field->type = $_POST["type"]; $field->type_options = $_POST["type_options"]; $field->position = $_POST["position"]; $field->visibility = $this->get_visibility_val(); $field->capabilities = self::handle_capabilities(); if (MPC::is_create()) { $field->insert(); } else if (MPC::is_edit()) { $field->update(MasterPress::$id); } if ($field->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_meta($field, $_POST["name_original"]); } // update other menu positions $op = $_POST["other_position"]; if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("fields"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } return $field; } return false; }
public function submit() { global $wpdb; $action = MasterPress::$action; if ($action == "create" || $action == "edit") { $builtin = $_POST["_builtin"] == "true"; $external = $_POST["_external"] == "true"; if ($builtin) { $post_type = MPM_PostType::find_by_name($_POST["name"]); $post_type->supports = implode(",", $_POST["supports"]); $post_type->disabled = isset($_POST["disabled"]); $post_type->visibility = $this->get_visibility_val("sites"); $post_type->columns = $_POST["columns"]; $post_type->labels = $_POST["labels"]; $post_type->menu_icon = self::handle_icon("menu_icon", "menu_icon_select"); if ($_POST["name"] == "page" && $post_type->menu_icon == "") { $post_type->menu_icon = "menu-icon-pages.png"; // restore default icon } if ($_POST["name"] == "post" && $post_type->menu_icon == "") { $post_type->menu_icon = "menu-icon-posts.png"; // restore default icon } if ($action == "edit") { $post_type->update(MasterPress::$id); } } else if ($external) { $post_type = MPM_PostType::find_by_name($_POST["name"]); $post_type->columns = $_POST["columns"]; $post_type->menu_icon = self::handle_icon("menu_icon", "menu_icon_select"); if ($action == "edit") { $post_type->update(MasterPress::$id); } } else { $post_type = new MPM_PostType(true); // consume the post data $post_type->name = $_POST["name"]; $post_type->plural_name = $_POST["plural_name"]; $post_type->disabled = isset($_POST["disabled"]); $post_type->labels = $_POST["labels"]; $post_type->description = $_POST["description"]; $post_type->publicly_queryable = isset($_POST["publicly_queryable"]); $post_type->exclude_from_search = isset($_POST["exclude_from_search"]); $post_type->show_ui = isset($_POST["show_ui"]); $post_type->show_in_menu = isset($_POST["show_in_menu"]); $post_type->hierarchical = isset($_POST["hierarchical"]); $post_type->menu_position = $_POST["menu_position"]; $post_type->menu_sub_position = $_POST["menu_sub_position"]; $post_type->menu_icon = self::handle_icon("menu_icon", "menu_icon_select"); $post_type->manage_sort_order = $_POST["manage_sort_order"]; $cap_type = $_POST["capability_type"]; if ($cap_type == "specific") { $post_type->capability_type = $_POST["name"]; } else if ($cap_type == "custom" && trim($_POST["capability_type_custom_value"]) != "") { $post_type->capability_type = $_POST["capability_type_custom_value"]; } else { $post_type->capability_type = $cap_type; } $post_type->capabilities = MPC::post_val("capabilities"); $post_type->map_meta_cap = isset($_POST["map_meta_cap"]); $post_type->hierarchical = isset($_POST["hierarchical"]); $post_type->supports = MPC::post_implode_val("supports"); $post_type->permalink_epmask = $_POST["permalink_epmask"]; $post_type->has_archive = isset($_POST["has_archive"]); $post_type->visibility = $this->get_visibility_val("sites,post_types"); $post_type->show_in_menu = isset($_POST["show_in_menu"]); $rewrite = array( "slug" => $_POST["rewrite"]["slug"], "with_front" => isset($_POST["rewrite"]["with_front"]), "feeds" => isset($_POST["rewrite"]["feeds"]) ); $post_type->rewrite = $rewrite; $post_type->query_var = $_POST["query_var"]; $post_type->can_export = isset($_POST["can_export"]); $post_type->show_in_nav_menus = isset($_POST["show_in_nav_menus"]); $post_type->_builtin = false; $post_type->_external = $_POST["_external"] == "true"; $post_type->columns = $_POST["columns"]; if ($action == "create") { $post_type->insert(); } else if ($action == "edit") { $post_type->update(MasterPress::$id); if ($post_type->is_valid()) { global $meow_provider; $meow_provider->migrate_post_type($post_type, $_POST["name_original"]); } } } if ($post_type->is_valid() && !$external) { // auto-generate a sprite icon MPU::create_icon_sprite($post_type->menu_icon, "", true); // attach any taxonomies and shared field sets $post_type->unlink_taxonomies(); if (MPC::post_val("taxonomies") != "" && count(MPC::post_val("taxonomies"))) { $post_type->link_taxonomies( MPM_Taxonomy::find_by_name_in( $_POST["taxonomies"] ) ); } // update the menu positions of other post types $omp = MPC::post_val("other_menu_position"); $omsp = MPC::post_val("other_menu_sub_position"); if (isset($omp) && is_array($omp)) { foreach ($omp as $name => $position) { $wpdb->update(MPM::table("post-types"), array( "menu_position" => $position, "menu_sub_position" => $omsp[$name] ), array( "name" => $name ), "%d", "%s" ); } } MasterPress::flag_for_rewrite(); } return $post_type; } else if ($action == "delete") { $pt = MPM_PostType::find_by_id(MasterPress::$id); $pt->delete( array( "posts" => $_POST["posts"], "posts_reassign_type" => $_POST["posts_reassign_type"], "field_sets" => $_POST["field_sets"], "field_data" => $_POST["field_data"] ) ); MasterPress::flag_for_rewrite(); return true; } else if ($action == "create-field-set" || $action == "edit-field-set") { $field_set = new MPM_FieldSet(); // consume the post data $field_set->name = $_POST["name"]; $field_set->singular_name = $_POST["singular_name"]; $field_set->disabled = isset($_POST["disabled"]); $field_set->labels = $_POST["labels"]; $field_set->allow_multiple = isset($_POST["allow_multiple"]); $field_set->visibility = $_POST["visibility"]; $field_set->capabilities = self::handle_capabilities(); $field_set->type = "p"; // p = shared $field_set->position = $_POST["position"]; $field_set->icon = self::handle_icon("icon", "icon_select"); $field_set->expanded = isset($_POST["expanded"]); $field_set->sidebar = isset($_POST["sidebar"]); $field_set->versions = $_POST["versions"]; $field_set->visibility = $this->get_visibility_val("sites,templates,post_types"); $post_type = MPM_PostType::find_by_id(MasterPress::$parent); // inform the validation of the post type $field_set->meta("post_type", $post_type); if (MPC::is_create()) { $field_set->insert(); } else if (MPC::is_edit()) { $field_set->update(MasterPress::$id); } if ($field_set->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_set_meta($field_set, $_POST["name_original"]); } // update other menu positions if (isset($_POST["other_position"])) { $op = $_POST["other_position"]; if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("field-sets"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } } return $field_set; } else if ($action == "delete-field-set") { $fg = MPM_FieldSet::find_by_id(MasterPress::$id); if ($fg) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $fg->delete_meta(); } } $fg->delete(); return true; } else if ($action == "delete-field") { $f = MPM_Field::find_by_id(MasterPress::$id); if ($f) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $f->delete_meta(); } } $f->delete(); return true; } else if ($action == "create-field" || $action == "edit-field") { // FIELD OPERATIONS (NOT FIELD GROUPS!) $field = new MPM_Field(); // consume the post data $field->field_set_id = $_POST["parent"]; $field->name = $_POST["name"]; $field->disabled = isset($_POST["disabled"]); $field->summary_options = $_POST["summary_options"]; $field->required = isset($_POST["required"]); $field->labels = $_POST["labels"]; $field->type = $_POST["type"]; $field->icon = self::handle_icon("icon", "icon_select"); if (isset($_POST["type_options"])) { $field->type_options = $_POST["type_options"]; } $field->position = $_POST["position"]; $field->visibility = $this->get_visibility_val("sites,templates,post_types"); $field->capabilities = self::handle_capabilities(); $fg = MPM_FieldSet::find_by_id($_POST["parent"]); if (MPC::is_create()) { $field->insert(); } else if (MPC::is_edit()) { $field->update(MasterPress::$id); } if ($field->is_valid()) { // update other menu positions if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_meta($field, $_POST["name_original"]); } if (isset($_POST["other_position"])) { $op = $_POST["other_position"]; if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("fields"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } } return $field; } return false; }
protected function import_field_sets(&$field_sets, $lookup_criteria = '', $dir = '') { $import = array(); $icon_dest = MASTERPRESS_CONTENT_MENU_ICONS_DIR; $icon_src = $dir.WOOF_DIR_SEP."icons".WOOF_DIR_SEP; if (isset($field_sets) && count($field_sets)) { foreach ($field_sets as $field_set) { // try to find a field set with the same specification // we need to UPDATE these, rather than creating a new ID, so that all relationships still hold $where = "`name` = '".$field_set["name"]."' ".$lookup_criteria; $data = $field_set; unset($data["id"], $data["fields"], $data["parent_id"], $data["description"], $data["templates_permission"], $data["sites"], $data["templates"]); $results = MPM_FieldSet::find( array( "where" => $where ) ); $field_set_insert = true; if (count($results)) { $field_set_insert = false; $field_set_model = $results[0]; } else { $field_set_model = new MPM_FieldSet(); } $field_set_model->set($data, true); if ($field_set_insert) { $field_set_model->insert(); } else { $field_set_model->update(); } $field_set["fields_by_name"] = array(); // install field set icons if (isset($field_set["icon"]) && !is_null($field_set["icon"]) && $field_set["icon"] != "") { $src = $icon_src.$field_set["icon"]; if (file_exists($src)) { copy($src, $icon_dest.$field_set["icon"]); } } // now run through all of the fields inside the set foreach ($field_set["fields"] as $field) { $data = $field; unset($data["id"], $data["description"], $data["parent_id"], $data["allow_multiple"], $data["tooltip_help"], $data["post_types_inherit"], $data["sites"]); // if the field set already existed, we need to look up a field with the same name under that parent // it's very important to UPDATE these, rather than creating a new ID, so that all meta pointers are still correct $field_insert = true; $field_model = new MPM_Field(); if ($field_set_insert) { $where = "`name` = '".$field["name"]."' AND `field_set_id` = ".$field_set_model->id; $results = MPM_Field::find(array( "where" => $where )); if (count($results)) { $field_model = $results[0]; $field_insert = false; } } $field_model->set($data, true); $field_model->field_set_id = $field_set_model->id; if (isset($field["icon"]) && !is_null($field["icon"]) && $field["icon"] != "") { $src = $icon_src.$field["icon"]; if (file_exists($src)) { copy($src, $icon_dest.$field["icon"]); } } if ($field_insert) { $field_model->insert(); } else { $field_model->update(); } $field_set["fields_by_name"][$field["name"]] = $field; } $import[$field_set["name"]] = $field_set; } } return $import; }
public function submit() { global $wpdb; $action = MasterPress::$action; if ($action == "create" || $action == "edit") { $field_set = new $this->model_class(); // consume the post data $field_set->name = $_POST["name"]; $field_set->singular_name = $_POST["singular_name"]; $field_set->disabled = isset($_POST["disabled"]); $field_set->labels = $_POST["labels"]; $field_set->visibility = MPC::post_val("visibility"); $field_set->capabilities = self::handle_capabilities(); $field_set->allow_multiple = isset($_POST["allow_multiple"]); $field_set->type = $this->db_type; $field_set->position = $_POST["position"]; $field_set->icon = self::handle_icon("icon", "icon_select"); $field_set->expanded = isset($_POST["expanded"]); $field_set->sidebar = isset($_POST["sidebar"]); $field_set->versions = $_POST["versions"]; $field_set->visibility = $this->get_visibility_val(); if ($action == "create") { $field_set->insert(); } else if ($action == "edit") { $field_set->update(MasterPress::$id); } if ($field_set->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_set_meta($field_set, $_POST["name_original"]); } // update other menu positions $op = MPC::post_val("other_position"); if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("field-sets"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } } return $field_set; } else if ($action == "delete") { $fg = MasterPress::$view->parent = call_user_func_array( array($this->model_class, "find_by_id"), array(MasterPress::$id)); if ($fg) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $fg->delete_meta(); } } $fg->delete(); return true; } else if ($action == "delete-field") { $f = MPM_Field::find_by_id(MasterPress::$id); if ($f) { $field_data_action = $_POST["field_data"]; if ($field_data_action == "delete") { $f->delete_meta(); } } $f->delete(); return true; } else if ($action == "create-field" || $action == "edit-field") { // FIELD OPERATIONS (NOT FIELD GROUPS!) $field = new MPM_Field(); // consume the post data $field->field_set_id = $_POST["parent"]; $field->name = $_POST["name"]; $field->disabled = isset($_POST["disabled"]); $field->summary_options = $_POST["summary_options"]; $field->required = isset($_POST["required"]); $field->labels = $_POST["labels"]; $field->type = $_POST["type"]; $field->icon = self::handle_icon("icon", "icon_select"); $field->type_options = $_POST["type_options"]; $field->position = $_POST["position"]; $field->visibility = $this->get_visibility_val(); $field->capabilities = self::handle_capabilities(); if (MPC::is_create()) { $field->insert(); } else if (MPC::is_edit()) { $field->update(MasterPress::$id); } if ($field->is_valid()) { if (MPC::is_edit()) { global $meow_provider; $meow_provider->migrate_field_meta($field, $_POST["name_original"]); } // update other menu positions $op = MPC::post_val("other_position"); if (isset($op) && is_array($op)) { foreach ($op as $id => $position) { $wpdb->update(MPM::table("fields"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); } } // clear the parent for the redirect MasterPress::$parent = null; } return $field; } }
public static function upload_field() { global $wf; $user = $wf->the_user(); if (is_user_logged_in()) { // work out the directory - for now, we'll store files in a directory that's named after the post type, but we'll make this more // flexible later on // check if this user has permission to uplaod if (!$user->can("upload_files")) { self::ajax_error(__("You do not have permission to upload files", MASTERPRESS_DOMAIN)); } MPM::incl("field"); $model_id = $_GET["model_id"]; $field = MPM_Field::find_by_id($model_id); list($dir, $sub) = self::upload_dir($field); $type_options = $field->type_options; $options = array( "sub_dir" => $sub, "dir" => $dir, "allowed_extensions" => $type_options["allowed_types"], "filename_case" => $type_options["filename_case"], "filename_sanitize" => $type_options["filename_sanitize"] ); if ($type_options["allowed_maxsize"] != "") { $options["size_limit"] = $type_options["allowed_maxsize"]."M"; } $uploader = new qqFileUploader($options); $result = $uploader->handleUpload(); // to pass data through iframe you will need to encode all html tags echo htmlspecialchars(json_encode($result), ENT_NOQUOTES); } else { self::ajax_error(__("You do not have permission to upload", MASTERPRESS_DOMAIN)); } }
public function post_type_field_sets($post_type_name, $template_name = "", $orderby = "position ASC") { global $wf; if (is_null($template_name)) { $template_name = ""; } $args = array($post_type_name, $template_name, $orderby); if (is_null($ret = $this->property_cache_args("post_type_field_sets", $args ))) { $this->field_types = array(); global $wpdb; $field_sets_table = MPM::table("field-sets"); $fields_table = MPM::table("fields"); $select_list = $this->assigned_select_list(); $sql = " SELECT $select_list FROM $field_sets_table s INNER JOIN $fields_table f ON s.id = f.`field_set_id` WHERE (s.type = 'p' OR s.type = 's') AND ".MPM::visibility_rlike("post_types", $post_type_name, "s.")." AND ".MPM::visibility_rlike("post_types", $post_type_name, "f."); if ($template_name != "") { // filter by template too $sql .= " AND ".MPM::visibility_rlike("templates", $template_name, "s.")." AND ".MPM::visibility_rlike("templates", $template_name, "f."); } $sql .= " AND f.disabled = 0 AND s.disabled = 0 ORDER BY set_type, set_position, field_position; "; $field_sets = array(); $fields_by_set = array(); $results = $wpdb->get_results($sql); if ($template_name != "") { $template_results = $this->template_field_sets($template_name); if ($template_results && count($template_results)) { $results = array_merge($results, $template_results); } } foreach ($results as $result) { $set_name = $result->set_name; $set_type = $result->set_type; $use_set = true; $use_field = true; if (isset($field_sets[$set_name])) { if ($set_type == $field_sets[$set_name]["type"]) { $use_set = false; // we already have this set recorded } if ($set_type == 't' && in_array($field_sets[$set_name]["type"], array('s','p'))) { // clear any existing fields already set $fields_by_set[$set_name] = null; } if ($set_type == 's' && $field_sets[$set_name]["type"] == 'p') { // the existing set is a post type field set, which holds greater specificity than this shared set, so don't use this set or field $use_set = false; $use_field = false; } if (in_array($set_type, array('p','s')) && $field_sets[$set_name]["type"] == 't') { // the existing set is a template field set, which holds greater specificity than this shared or post type set, so don't use this set's info $use_set = false; $use_field = false; } } if ($use_set) { $field_sets[$set_name] = $this->set_data_from_result($result); } // now record the field too if ($use_field) { if (!in_array($result->field_type, $this->field_types)) { $this->field_types[] = $result->field_type; } $fields_by_set[$set_name][$result->field_name] = $this->field_data_from_result($result); } } // endforeach $field_set_models = array(); foreach ($field_sets as $field_set) { if ($field_set["type"] == "s") { $field_set_model = new MPM_SharedFieldSet(); } else { $field_set_model = new MPM_FieldSet(); } $field_set_model->set_from_row($field_set); $field_models = array(); $fields = $fields_by_set[$field_set["name"]]; if (is_array($fields) && count($fields)) { foreach ($fields as $field) { $field_model = new MPM_Field(); $field_model->set_from_row($field); if ($field_model->in_current_site()) { $field_models[$field["name"]] = $field_model; } } $field_set_model->set_fields($field_models); } if ($field_set_model->in_current_site()) { $field_set_models[$field_set_model->name] = $field_set_model; } } $ret = $this->property_cache_args("post_type_field_sets", $args, $field_set_models ); } return $ret; }
public static function save_meta($object_id, $object_type) { global $wpdb, $meow_provider; global $wf; do_action("mp_pre_save_meta", $object_id, $object_type); if (apply_filters("mp_do_save_meta", $do = true, $object_id, $object_type)) { MPM::incl("field"); $object_type_type = ""; $current_data = array(); if ($object_type == "post") { // check the edit post meta cap for this type $cap = "edit_post"; $post = $wf->post($object_id); if ($post->exists()) { $type = $post->type(); $cap = $type->cap("edit_post", "edit_post"); // update the template meta if ($type->supports("mp-page-attributes")) { if (isset($_POST["page_template"])) { $template = $_POST["page_template"]; if ($template == "") { delete_post_meta($object_id, "_wp_page_template"); } else { update_post_meta($object_id, "_wp_page_template", $template); } } } } if ( !current_user_can( $cap, $object_id ) ) { return $object_id; } } else if ($object_type == "term") { // check the edit terms cap for the taxonomy $cap = "manage_categories"; $taxonomy_name = self::infer_taxonomy_name(); $tax = $wf->taxonomy($taxonomy_name); $object_type_type = $taxonomy_name; if ($tax->exists()) { $cap = $tax->cap("edit_terms", "manage_categories"); } if ( !current_user_can( $cap ) ) { return $object_id; } } else if ($object_type == "user") { if ( !current_user_can( 'edit_users' ) ) { return $object_id; } } else if ($object_type == "site") { if ( !current_user_can( 'edit_posts' ) ) { return $object_id; } } $meta = self::post_val('mp_meta', array()); $meta_order = self::post_val('mp_meta_order', array()); $meta_model = self::post_val('mp_meta_model', array()); $meta_prop = self::post_val('mp_meta_prop', array()); $meta_field_ids = self::post_val('mp_meta_field_ids', array()); $meta_dirty = self::post_val('mp_meta_dirty', array()); $meta_versions = self::post_val('mp_meta_versions', array()); $field_models = array(); if (is_array($meta_field_ids)) { $meta_field_ids = array_unique($meta_field_ids); $results = MPM_Field::find_by_id_in($meta_field_ids); foreach ($results as $field) { $field_models[$field->id] = $field; } } $wpdb->show_errors(); if (isset($_POST["mp_meta_model"])) { if ($object_type == "post") { if ( $the_post = wp_is_post_revision( $object_id ) ) { $object_id = $the_post; } } try { $meow_provider->create_version($object_id, $object_type, $object_type_type, "initial", $meta_versions); } catch( Exception $e) { // silently catch, we REALLY don't want this to prevent a data save if something goes wrong! } /* Delete the old post meta */ foreach ( $meta_model as $set_name => $fields ) { foreach ($fields as $field_name => $model_id) { $field = $field_models[$model_id]; $meta_name = $set_name.".".$field_name; self::delete_object_meta($object_type, $object_id, $meta_name); // now go through the properties too $dont_care = MPFT::type_class($field->type); foreach (MPFT::type_properties($field->type) as $prop) { self::delete_object_meta($object_type, $object_id, $meta_name.":".$prop); } } } $model_id_prop_stored = array(); // Create the new values foreach( $meta as $set_name => $set_items ) { $set_index = 0; if (is_array($set_items)) { foreach ($set_items as $fields) { $set_index++; foreach ($fields as $field_name => $value) { // here the field type should prepare the value, if necessary // grab the type $model_id = $meta_model[$set_name][$field_name]; $model = $field_models[$model_id]; $val = MPU::db_encode($value); if ($model) { if ($type_class = MPFT::type_class($model->type)) { $val = MPU::db_encode(call_user_func_array( array($type_class, "value_for_save"), array($value, $model))); } // create the post meta $meta_name = MPFT::meta_key($set_name, $field_name); self::add_object_meta($object_type, $object_id, "{$meta_name}", $val); // now store the properties foreach (MPFT::type_properties($model->type) as $prop) { if ($prop == "field_id" && !isset($model_id_prop_stored[$model_id])) { $model_id_prop_stored[$model_id] = true; self::add_object_meta($object_type, $object_id, "{$meta_name}:{$prop}", $model_id); } else { $prop_set_index = $meta_order[$set_name][$set_index]; if (isset($meta_prop[$set_name][(int) $prop_set_index][$field_name][$prop])) { $prop_value = $meta_prop[$set_name][(int) $prop_set_index][$field_name][$prop]; } if (isset($prop_value)) { self::add_object_meta($object_type, $object_id, "{$meta_name}:{$prop}", $prop_value); } } } } } // endforeach $fields // fill in blanks for any values that weren't submitted (this happens with checkboxes that are not checked) foreach ($meta_model[$set_name] as $field_name => $model_id) { if (!isset($fields[$field_name])) { $meta_name = MPFT::meta_key($set_name, $field_name); self::add_object_meta($object_type, $object_id, "{$meta_name}", ""); // now store the properties for blanks (if required) foreach (MPFT::type_properties($field->type) as $prop) { if ($prop == "field_id" && !isset($model_id_prop_stored[$model_id])) { $model_id_prop_stored[$model_id] = true; self::add_object_meta($object_type, $object_id, "{$meta_name}:{$prop}", $model_id); } else { if (isset($meta_prop[$set_name][$set_index][$field_name][$prop])) { $prop_value = $meta_prop[$set_name][$set_index][$field_name][$prop]; self::add_object_meta($object_type, $object_id, "{$meta_name}:{$prop}", $prop_value); } } } } } } } // endif is_array(set_items) } // foreach $set_name => $set_items // create the current content version if (!self::$version_saved) { try { $meow_provider->create_version($object_id, $object_type, $object_type_type, $meta_dirty, $meta_versions); } catch( Exception $e) { // silently catch, we REALLY don't want this to prevent a data save if something goes wrong! } self::$version_saved = true; } } // isset $_POST["mp_meta_model"] } do_action("mp_after_save_meta", $object_id, $object_type); }
public function query_fields($args = array()) { $r = wp_parse_args( array( "where" => "`field_set_id` = '{$this->id}'" ), $args ); return MPM_Field::find( $r ); }