PHP WordPress Plugin


<?php
/*
Plugin Name: Web App
Plugin URI: http://www.learnenglishcloud.com
Description: Create lessons 
Version: 1.0
Author: Ernest Smuga
*/

add_action( 'init', 'create_webapp' );


function create_webapp() {
    register_post_type( 'Classes',
        array(
            'labels' => array(
                'name' => 'Web App Creator',
                'singular_name' => 'Web App Creator',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Class',
                'edit' => 'Edit',
                'edit_item' => 'Edit Class *2 update',
                'new_item' => 'New Web App Creator',
                'view' => 'View',
                'view_item' => 'View Class',
                'search_items' => 'Search Classes',
                'not_found' => 'No Classes found',
                'not_found_in_trash' => 'No Classes found in Trash',
                'parent' => 'Parent Label web-app.php'
            ),
 
            'public' => true,
            'menu_position' => 15,
            'supports' => array( 'title'),
            'taxonomies' => array( 'category' ),
            /*'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),*/
            /*'has_archive' => true*/
        )
    );
}


add_action( 'admin_init', 'my_admin' );

function my_admin() {
    add_meta_box( 'level_word_list_meta_box',
        'Level',
        'level_dynamic_inner_custom_box',
        'Classes', 'normal', 'high'
    );
    
    add_meta_box( 'level_app_types_meta_box',
        'App Types',
        'level_app_types_dynamic_inner_custom_box',
        'Classes', 'normal', 'high'
    );
    add_meta_box( 'word_list_meta_box',
        'Word List',
        'dynamic_inner_custom_box',
        'Classes', 'normal', 'high'
    );
    add_meta_box( 'video_list_meta_box',
        'Video List',
        'video_dynamic_inner_custom_box',
        'Classes', 'normal', 'high'
    );
}


/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );


/* Prints the box content */
function level_dynamic_inner_custom_box() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
    ?>
    <div id="meta_inner">
    <?php

    //get the saved meta as an arry
    $level = get_post_meta($post->ID,'level',true);

    if ( isset( $level )) {
            	printf( '<input type="text" name="level" value="%1$s" />', $level );    
            } else {
            	printf('<input type="text" name="level" value="" />');
            }        
    ?>
    </div>
    <?php          
}

/* Prints the box content */
function level_app_types_dynamic_inner_custom_box() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
    ?>
    <div id="meta_inner">
    <?php

    //get the saved meta as an arry
    $app = get_post_meta($post->ID,'app',true);
    $video = get_post_meta($post->ID,'video',true);
    $cards = get_post_meta($post->ID,'cards',true);
    $quiz = get_post_meta($post->ID,'quiz',true);

    
            	printf( 'app: <input id="app" type="checkbox" name="app"  value="%1$s"/>', $app);
            	printf( 'video: <input id="video" type="checkbox" name="video" value="%1$s"/>', $video );
            	printf( 'cards: <input id="cards" type="checkbox" name="cards" value="%1$s"/>', $cards );
            	printf( 'quiz: <input id="quiz" type="checkbox" name="quiz" value="%1$s"/>', $quiz );
            	
    ?>
    </div>
    <?php          
}

/* Prints the box content */
function dynamic_inner_custom_box() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
    ?>
    <div id="meta_inner">
    <?php

    //get the saved meta as an arry
    $class = get_post_meta($post->ID,'class',true);

    $c = 0;
    if(is_array($class)) {
        foreach( $class as $word ) {
            if ( isset( $word['text'] ) || isset( $word['image'] ) || isset( $word['sound'] ) ) {
                printf( '<p>text: <input type="text" name="class[%1$s][text]" value="%2$s" /> image: <input type="text" name="class[%1$s][image]" value="%3$s" /><br /> sound: <input type="text" name="class[%1$s][sound]" value="%4$s" /><span class="remove">%5$s</span></p><br />', $c, $word['text'], $word['image'], $word['sound'],__( 'Remove word' ) );
                $c = $c +1;
                
            }
        }
    }

    ?>
    
<span id="here"></span>
<span class="add"><?php _e('Add words'); ?></span>
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
    
         $( "input[type=checkbox]" ).on( "click", function() {
            if($(this).is(":checked")){
                $( this ).val( "true" );
            }else{
                $( this ).val( "false" );
            }
         } );
         
        if ($('#app').val() == 'true'){$('#app').prop('checked', true);}
        if ($('#video').val() == 'true'){$('#video').prop('checked', true);}
        if ($('#cards').val() == 'true'){$('#cards').prop('checked', true);}
        if ($('#quiz').val() == 'true'){$('#quiz').prop('checked', true);}
        
        var count = <?php echo $c; ?>;
        
        $(".add").click(function() {            
            $('#here').append('<p>text: <input 	type="text" name="class['+count+'][text]" value="" /> image: <input type="text" name="class['+count+'][image]" value="" /> <br />sound: <input type="text" name="class['+count+'][sound]" value="" /><span class="remove">Remove word</span></p><br />' );
            count = count + 1;             
            return false;
        });
        $(".remove").live('click', function() {
            $(this).parent().remove();  
                    
        });
        
    });
    </script>
</div><?php

}

function video_dynamic_inner_custom_box() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
    ?>
    <div id="meta_inner">
    <?php

    //get the saved meta as an arry
    $video = get_post_meta($post->ID,'video',true);

    $c = 0;
    if(is_array($video)) {
        foreach( $video as $word ) {
            if ( isset( $word['text'] ) ) {
                printf( '<p>video: <input type="text" name="video[%1$s][text]" value="%2$s" /><span class="remove_video">%3$s</span></p><br />', $c, $word['text'], __( 'Remove word' ) );
                $c = $c +1;
            }
        }
    }

    ?>
<span id="video"></span>
<span class="add_video"><?php _e('Add words'); ?></span>
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
    
        var count = <?php echo $c; ?>;
        $(".add_video").click(function() {
            count = count + 1;

            $('#video').append('<p>video: <input type="text" name="video['+count+'][text]" value="" /><span class="remove_video">Remove word</span></p><br />' );
            return false;
        });
        $(".remove_video").live('click', function() {
            $(this).parent().remove();
        });
    });
    </script>
</div><?php

}

/* When the post is saved, saves our custom data */
function dynamic_save_postdata( $post_id ) {
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !isset( $_POST['dynamicMeta_noncename'] ) )
        return;

    if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
        return;

    // OK, we're authenticated: we need to find and save the data
    
    $app= $_POST['app'];
    update_post_meta($post_id,'app',$app);
    $video= $_POST['video'];
    update_post_meta($post_id,'video',$video);
    $cards= $_POST['cards'];
    update_post_meta($post_id,'cards',$cards);
    $quiz= $_POST['quiz'];
    update_post_meta($post_id,'quiz',$quiz);
    
    $class = $_POST['class'];

    update_post_meta($post_id,'class',$class);
    
    $video = $_POST['video'];

    update_post_meta($post_id,'video',$video);
    
    $level = $_POST['level'];

    update_post_meta($post_id,'level',$level);
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>