array("default" => array() ), ); // get all buttons in collection, set as proper button object/ function getButtons() { // should maybe but a 'light init' of buttons? $selection = $this->data["selection"]; $buttonsArray = array(); if (count($selection) == 0) return $buttonsArray; $table = maxUtils::get_table_name(); $status = 'publish'; $query_selection = $selection; $selectmask = implode( ', ', array_fill( 0, count( $query_selection ), '%d' ) ); $sql = "SELECT DISTINCT * FROM {$table} WHERE id in (" . $selectmask . ") and status = '%s' "; $prepare_values = $query_selection; array_push($prepare_values, $status); global $wpdb; $sql = $wpdb->prepare($sql, $prepare_values ); $results = $wpdb->get_results($sql, ARRAY_A); $data_array = array(); foreach ($results as $result) { $data_array[$result['id']] = $result; } foreach($selection as $button_id) { $button = MB()->getClass("button"); // performance: bypass the set ( = query ) for every button //$button->set($button_id); $data = isset($data_array[$button_id]) ? $data_array[$button_id] : array(); if (count($data) > 0) { maxButtons::buttonLoad(array("button_id" => $button_id)); // central registration - from button $button->setupData($data); } else continue; // non-existing buttons should not show. maxButtons::forceNextID(); $buttonsArray[] = $button; } return $buttonsArray; } function parse($domObj, $args) { $buttons = $this->collection->getLoadedButtons(); $output = ''; $collectionObj = $domObj->find('.maxcollection', 0); $btn_display = ''; foreach($buttons as $button) { //$button->reloadData(); $button_id = $button->getID(); $document_id = $button->getDocumentID(); $default_args = array("mode" => "normal", "compile" => false, "load_type" => "footer", ); $button_args = wp_parse_args($args, $default_args); $button_args["load_css"] = $button_args["load_type"]; $button_args["echo"] = false; // non-optional. if (isset($args['preview']) && $args["preview"]) $button_args["mode"] = "preview"; // buttons work with mode. $btn_display .= ""; } $collectionObj->innertext = $btn_display; $domObj->load($domObj->save()); return $domObj; } function save_fields($data, $post) { $blockdata = $this->data; $selection = array(); $button = MB()->getClass("button"); if (isset($post["sorted"])) { $sorted = array_filter(explode(",",sanitize_text_field($post["sorted"]))); $i = 0; foreach($sorted as $button_id) { if (intval($button_id) > 0) { $selection[$i] = $button_id; $i++; $set_bool = $button->set($button_id); if (! $set_bool) continue; // buttons that don't exist, like virtuals. $button_data = $button->get(); $collection_id = $this->collection->getID(); $collection_meta = isset($button_data["meta"]["in_collections"]) ? $button_data["meta"]["in_collections"] : array(); if (! is_array($collection_meta)) $collection_meta = maybe_unserialize($collection_meta); if (is_array($collection_meta)) { $key = array_search($collection_id, $collection_meta); if ($key === false) { $collection_meta[] = $collection_id; $button_data["meta"]["in_collections"] = $collection_meta; $button->update($button_data); } } } } } /* If a button id is in the previous selection, but not in the current, maybe the button should be deleted if the button appears to be auto-generated only for this collection */ if (isset($post["previous_selection"])) { $previous = array_filter(explode(",",sanitize_text_field($post["previous_selection"]))); foreach($previous as $button_id) { if (! in_array($button_id, $selection) && intval($button_id) > 0) $this->collection->maybe_delete_button($button_id); } } $data[$this->blockname]["selection"] = $selection; return $data; } function admin_fields() { extract($this->data); // admin data $btns = $this->collection->editor_getPacks(); ?>