Permalink
Browse files

Changed class name to Wordpress_Creation_Kit

  • Loading branch information...
1 parent 60c5a4d commit 1844a64b7e553d6327bc77590222d9d8b724a01a @madalinungureanu committed Jan 5, 2012
Showing with 102 additions and 44 deletions.
  1. +1 −1 wordpress-creation-kit.js
  2. +101 −43 wordpress-creation-kit.php
@@ -107,7 +107,7 @@ function removeMeta(value, id, element_id, nonce){
/* reorder elements through drag and drop */
function mb_sortable_elements() {
- jQuery( ".mb-table-container tbody" ).not( jQuery( ".mb-table-container.single tbody" ) ).sortable({
+ jQuery( ".mb-table-container tbody" ).not( jQuery( ".mb-table-container.single tbody, .mb-table-container.not-sortable tbody" ) ).sortable({
update: function(event, ui){
var value = jQuery(this).parent().prev().attr('id');
View
@@ -55,7 +55,9 @@ class Wordpress_Creation_Kit{
'page_template' => '',
'post_id' => '',
'single' => false,
- 'wpml_compatibility' => false
+ 'wpml_compatibility' => false,
+ 'sortable' => true,
+ 'context' => 'post_meta'
);
private $args;
@@ -108,42 +110,50 @@ function __construct( $args ) {
//add metabox using wordpress api
function wck_add_metabox() {
-
- if( $this->args['post_id'] == '' && $this->args['page_template'] == '' )
- add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), $this->args['post_type'], 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array']) );
- else{
- $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
-
- if( $this->args['post_id'] != '' && $this->args['page_template'] != '' ){
- $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
- if( $this->args['post_id'] == $post_id && $template_file == $this->args['page_template'] )
- add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), 'page', 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array'] ) );
- }
+ if( $this->args['context'] == 'post_meta' ){
+ if( $this->args['post_id'] == '' && $this->args['page_template'] == '' )
+ add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), $this->args['post_type'], 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array']) );
else{
-
- if( $this->args['post_id'] != '' ){
- if( $this->args['post_id'] == $post_id ){
- $post_type = get_post_type( $post_id );
- add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), $post_type, 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array'] ) );
- }
+ $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
+
+ if( $this->args['post_id'] != '' && $this->args['page_template'] != '' ){
+ $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
+ if( $this->args['post_id'] == $post_id && $template_file == $this->args['page_template'] )
+ add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), 'page', 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array'] ) );
}
+ else{
- if( $this->args['page_template'] != '' ){
- $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
- if ( $template_file == $this->args['page_template'] )
- add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), 'page', 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array']) );
+ if( $this->args['post_id'] != '' ){
+ if( $this->args['post_id'] == $post_id ){
+ $post_type = get_post_type( $post_id );
+ add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), $post_type, 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array'] ) );
+ }
+ }
+
+ if( $this->args['page_template'] != '' ){
+ $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
+ if ( $template_file == $this->args['page_template'] )
+ add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), 'page', 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array']) );
+ }
+
}
- }
-
- }
-
+ }
+ }
+ else if( $this->args['context'] == 'option' ){
+ add_meta_box($this->args['metabox_id'], $this->args['metabox_title'], array( &$this, 'wck_content' ), $this->args['post_type'], 'normal', 'low', array( 'meta_name' => $this->args['meta_name'], 'meta_array' => $this->args['meta_array']) );
+ }
}
function wck_content($post, $metabox){
//output the add form
if( $this->args['single'] ){
- $meta_val = get_post_meta( $post->ID, $metabox['args']['meta_name'], true );
+
+ if( $this->args['context'] == 'post_meta' )
+ $meta_val = get_post_meta( $post->ID, $metabox['args']['meta_name'], true );
+ else if ( $this->args['context'] == 'option' )
+ $meta_val = get_option( $metabox['args']['meta_name'] );
+
if( empty( $meta_val ) )
self::create_add_form($metabox['args']['meta_array'], $metabox['args']['meta_name'], $post);
}
@@ -293,8 +303,13 @@ function mb_update_form($fields, $meta, $id, $element_id){
// create the $fields_myname variable dinamically so we can use the global one
//$fields = 'fields_'.$meta;
//global $$fields;
+
+ if( $this->args['context'] == 'post_meta' )
+ $results = get_post_meta($id, $meta, true);
+ else if ( $this->args['context'] == 'option' )
+ $results = get_option( $meta );
+
- $results = get_post_meta($id, $meta, true);
$nr = count($results[$element_id])+4;
$form = '';
$form .= '<tr id="update_container_'.$meta.'_'.$element_id.'"><td colspan="'.$nr.'">';
@@ -345,14 +360,18 @@ function mb_update_form($fields, $meta, $id, $element_id){
function wck_output_meta_content($meta, $id, $fields){
$edit_nonce = wp_create_nonce( 'wck-edit-entry' );
- $delete_nonce = wp_create_nonce( 'wck-delete-entry' );
+ $delete_nonce = wp_create_nonce( 'wck-delete-entry' );
- $results = get_post_meta($id, $meta, true);
+ if( $this->args['context'] == 'post_meta' )
+ $results = get_post_meta($id, $meta, true);
+ else if ( $this->args['context'] == 'option' )
+ $results = get_option( $meta );
$list = '';
$list .= '<table id="container_'.esc_attr($meta).'" class="mb-table-container widefat';
if( $this->args['single'] ) $list .= ' single';
+ if( !$this->args['sortable'] ) $list .= ' not-sortable';
$list .= '" post="'.esc_attr($id).'">';
@@ -414,12 +433,21 @@ function wck_add_meta(){
$meta = $_POST['meta'];
$id = absint($_POST['id']);
$values = $_POST['values'];
- $results = get_post_meta($id, $meta, true);
+
+ if( $this->args['context'] == 'post_meta' )
+ $results = get_post_meta($id, $meta, true);
+ else if ( $this->args['context'] == 'option' )
+ $results = get_option( $meta );
+
$results[] = $values;
- update_post_meta($id, $meta, $results);
+
+ if( $this->args['context'] == 'post_meta' )
+ update_post_meta($id, $meta, $results);
+ else if ( $this->args['context'] == 'option' )
+ update_option( $meta, $results );
/* if wpml_compatibility is true add for each entry separete post meta for every element of the form */
- if( $this->args['wpml_compatibility'] ){
+ if( $this->args['wpml_compatibility'] && $this->args['context'] == 'post_meta' ){
$meta_suffix = count( $results );
$i=1;
@@ -439,13 +467,25 @@ function wck_update_meta(){
$id = absint($_POST['id']);
$element_id = $_POST['element_id'];
$values = $_POST['values'];
- $results = get_post_meta($id, $meta, true);
+
+
+
+ if( $this->args['context'] == 'post_meta' )
+ $results = get_post_meta($id, $meta, true);
+ else if ( $this->args['context'] == 'option' )
+ $results = get_option( $meta );
+
$results[$element_id] = $values;
- update_post_meta($id, $meta, $results);
+
+ if( $this->args['context'] == 'post_meta' )
+ update_post_meta($id, $meta, $results);
+ else if ( $this->args['context'] == 'option' )
+ update_option( $meta, $results );
+
/* if wpml_compatibility is true update the coresponding post metas for every element of the form */
- if( $this->args['wpml_compatibility'] ){
+ if( $this->args['wpml_compatibility'] && $this->args['context'] == 'post_meta' ){
$meta_suffix = $element_id + 1;
$i = 1;
@@ -492,17 +532,27 @@ function wck_remove_meta(){
$meta = $_POST['meta'];
$id = absint($_POST['id']);
$element_id = absint($_POST['element_id']);
- $results = get_post_meta($id, $meta, true);
+
+ if( $this->args['context'] == 'post_meta' )
+ $results = get_post_meta($id, $meta, true);
+ else if ( $this->args['context'] == 'option' )
+ $results = get_option( $meta );
+
$old_results = $results;
unset($results[$element_id]);
/* reset the keys for the array */
$results = array_values($results);
- update_post_meta($id, $meta, $results);
+
+ if( $this->args['context'] == 'post_meta' )
+ update_post_meta($id, $meta, $results);
+ else if ( $this->args['context'] == 'option' )
+ update_option( $meta, $results );
+
/* TODO: optimize so that it updates from the deleted element forward */
/* if wpml_compatibility is true delete the coresponding post metas */
- if( $this->args['wpml_compatibility'] ){
+ if( $this->args['wpml_compatibility'] && $this->args['context'] == 'post_meta' ){
$meta_suffix = 1;
@@ -553,20 +603,28 @@ function wck_remove_meta(){
function wck_reorder_meta(){
$meta = $_POST['meta'];
$id = absint($_POST['id']);
- $elements_id = $_POST['values'];
+ $elements_id = $_POST['values'];
+
+ if( $this->args['context'] == 'post_meta' )
+ $results = get_post_meta($id, $meta, true);
+ else if ( $this->args['context'] == 'option' )
+ $results = get_option( $meta );
- $results = get_post_meta($id, $meta, true);
$new_results = array();
foreach($elements_id as $element_id){
$new_results[] = $results[$element_id];
}
$results = $new_results;
- update_post_meta($id, $meta, $results);
+ if( $this->args['context'] == 'post_meta' )
+ update_post_meta($id, $meta, $results);
+ else if ( $this->args['context'] == 'option' )
+ update_option( $meta, $results );
+
/* if wpml_compatibility is true reorder all the coresponding post metas */
- if( $this->args['wpml_compatibility'] ){
+ if( $this->args['wpml_compatibility'] && $this->args['context'] == 'post_meta' ){
$meta_suffix = 1;
foreach( $new_results as $result ){

0 comments on commit 1844a64

Please sign in to comment.