First WordPress plugin

First WordPress plugin
/* Plugin Name: crud operation
 * Plugin URI: www.datum.com
 * description: a plugin for performing basic crud operations
 * Vesion: 1.0
 * Author URI: www.datum.com
 * license: GP2
 */
function crud_setUserCaps($param) {
    $editor = get_role(strtolower($param));
    $editor->add_cap(‘edit_download_item’);
    $editor->add_cap(‘read_download_item’);
    $editor->add_cap(‘delete_download_item’);
    $editor->add_cap(‘edit_download_items’);
    $editor->add_cap(‘delete_download_items’);
    $editor->add_cap(‘read_download_items’);
}
function crud_unsetUserCaps($param) {
    $editor = get_role(strtolower($param));
    $editor->remove_cap(‘edit_download_item’);
    $editor->remove_cap(‘read_download_item’);
    $editor->remove_cap(‘delete_download_item’);
    $editor->remove_cap(‘edit_download_items’);
    $editor->remove_cap(‘delete_download_items’);
    $editor->remove_cap(‘read_download_items’);
}
function set_caps() {
    //author,subscriber,contributor
    crud_setUserCaps(‘editor’);
    crud_setUserCaps(‘administrator’);
}
function crud_activationFun($param) {
    set_caps();
    crud_createDBTable();
    crud_installData();
    // Then flush them
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, ‘crud_activationFun’);
function crud_deactivationFun($param) {
    crud_dropTable();
    crud_unsetUserCaps(‘editor’);
    crud_unsetUserCaps(‘administrator’);
    flush_rewrite_rules();
}
register_deactivation_hook(__FILE__, ‘crud_deactivation_fun’);
add_action(‘wp_enqueue_scripts’, ‘crud_loadScripts’);
function crud_loadScripts($param) {
    wp_enqueue_script(‘jquery’);
    wp_register_script(‘docready’, plugins_url(‘js/docready.js’, __FILE__));
    wp_enqueue_script(‘docready’);
}
add_action(‘wp_enques_styles’, ‘crud_loadStyles’);
function crud_loadStyles($param) {
    wp_register_style(‘baseStyle’, plugins_url(‘css/style.css’, __FILE__));
    wp_enqueue_style(‘baseStyle’);
}
add_action(‘init’, ‘crud_addRewriteRules’);
function crud_addRewriteRules() {
    add_rewrite_rule(“download_items/^([^/]*)/?”, ‘index.php?post_type=download_items=$matches[1]’, ‘top’);
}
add_action(‘init’, ‘crud_registerDownloadItems’);
function crud_registerDownloadItems() {
    $labels = array(
        ‘menu_name’ => _x(‘download_items’, ‘Download Items’),
        ‘name’ => ‘Download Items’,
        ‘singular_name’ => ‘Download Item’,
        ‘add_new’ => ‘Add New’,
        ‘add_new_item’ => ‘Add New Download Item’,
        ‘edit_item’ => ‘Edit Download Item’,
        ‘new_item’ => ‘New Download Item’,
        ‘all_items’ => ‘All Download Items’,
        ‘view_item’ => ‘View Download Item’,
        ‘search_items’ => ‘Search Download Items’,
        ‘not_found’ => ‘No Download Items found’,
        ‘not_found_in_trash’ => ‘No Download Items found in Trash’,
        ‘parent_item_colon’ => ”,
        ‘menu_name’ => ‘Download Items’
    );
    $args = array(
        ‘labels’ => $labels,
        ‘hierarchical’ => true,
        ‘description’ => ‘Slideshows’,
        ‘supports’ => array(‘title’, ‘editor’),
        ‘public’ => true,
        ‘show_ui’ => true,
        ‘show_in_menu’ => true,
        ‘show_in_nav_menus’ => true,
        ‘publicly_queryable’ => true,
        ‘exclude_from_search’ => false,
        ‘has_archive’ => true,
        ‘query_var’ => true,
        ‘can_export’ => true,
        ‘rewrite’ => array(
            ‘slug’ => ‘download_items’,
            ‘with_front’ => false,
            ‘feed’ => true,
            ‘pages’ => true
        ),
        ‘capabilities’ => array(
            // Meta capabilities
            ‘edit_post’ => ‘edit_download_item’,
            ‘read_post’ => ‘read_download_item’,
            ‘delete_post’ => ‘delete_download_item’,
            // Primitive capabilities
            ‘edit_posts’ => ‘edit_download_items’,
            ‘delete_post’ => ‘delete_download_items’,
            ‘read_posts’ => ‘read_download_items’,
        ),
        ‘capability_type’ => ‘post’
    );
    register_post_type(‘download_items’, $args);
}

add_action(‘add_meta_boxes’, ‘crud_addMetaBoxes’);
function crud_addMetaBoxes() {
    //add_meta_box($id, $title, $callback, $page, $context, $priority);
    add_meta_box(“crud_custom_fields”, “Other Information”, ‘crud_viewFields’, “download_items”, “normal”);
}
function crud_viewFields() {
    global $post, $wpdb;
    $table_name = $wpdb->prefix . “categories”;
    $checked_status = ”;
    $download_price = get_post_meta($post->ID, ‘download_price’);
    $crud_category = get_post_meta($post->ID, ‘crud_category’);
    $myrows = $wpdb->get_results( “SELECT category_name, category_Label FROM {$table_name}” );
   
    $html = ”;
    $html .= ‘

‘;
    $html .= ”
               

               

             

             

               

               

             

           

“;
    $html .= ‘
    ‘;
       
        foreach ($myrows as $key => $value) {
            if (stristr($crud_category[0], $value->category_name) == true) {
                $checked_status = ‘ checked=”checked” ‘;
                $html .= ‘
  •                    
                   
  • ‘;
            } else {
                $html .= ‘

  •                    
                   
  • ‘;
            }
        }

        $html .= “

“;
    echo $html;
}
/* Save Slider Options to database */
add_action(‘save_post’, ‘crud_saveDownloadInfo’);
function crud_saveDownloadInfo($post_id) {

    // verify nonce
    if (!wp_verify_nonce($_POST[‘crud_custom_fields_nonce’], basename(__FILE__))) {
        return $post_id;
    }
    // check autosave
    if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) {
        return $post_id;
    }
    // check permissions
    if (‘download_items’ == $_POST[‘post_type’] && current_user_can(‘edit_download_items’, $post_id)) {
        $download_price = (isset($_POST[‘crud_download_price’]) ? $_POST[‘crud_download_price’] : ”);
        $categories = (isset($_POST[‘crud_category’]) ? $_POST[‘crud_category’] : ”);
        $categories = strip_tags(json_encode($categories));
        update_post_meta($post_id, “download_price”, $download_price);
        update_post_meta($post_id, “crud_category”, $categories);
    } else {
        return $post_id;
    }
}
// Callback function to show fields in meta box
add_filter(‘map_meta_cap’, ‘crud_downloadItemMetaCap’, 10, 4);
function crud_downloadItemMetaCap($primitive_caps, $meta_cap, $user_id, $args) {
    // If meta-capability is not download_item based do nothing.
    if (!in_array($meta_cap, array(‘edit_download_item’, ‘delete_download_item’, ‘read_download_item’))) {
        return $primitive_caps;
    }
    // Check post is of post type.
    $post = get_post($args[0]);
    $post_type = get_post_type_object($post->post_type);
    if (‘download_items’ != $post_type) {
        return $primitive_caps;
    }
    $primitive_caps = array();
    switch ($meta_cap):
        case ‘edit_download_item':
            if ($post->post_author == $user_id) {
                // User is post author
                if (‘publish’ == $post->post_status) {
                    // download_item is published: require ‘edit_published_download_items’ capability
                    $primitive_caps[] = $post_type->cap->edit_published_posts;
                } elseif (‘trash’ == $post->post_status) {
                    if (‘publish’ == get_post_meta($post->ID, ‘_wp_trash_meta_status’, true)) {
                        // download_item is a trashed published post require ‘edit_published_download_items’ capability
                        $primitive_caps[] = $post_type->cap->edit_published_posts;
                    }
                } else {
                    $primitive_caps[] = $post_type->cap->edit_posts;
                }
            } else {
                // The user is trying to edit a post belonging to someone else.
                $primitive_caps[] = $post_type->cap->edit_others_posts;
                // If the post is published or private, extra caps are required.
                if (‘publish’ == $post->post_status) {
                    $primitive_caps[] = $post_type->cap->edit_published_posts;
                } elseif (‘private’ == $post->post_status) {
                    $primitive_caps[] = $post_type->cap->edit_private_posts;
                }
            }
            break;
        case ‘read_download_item':
            if (‘private’ != $post->post_status) {
                // If the post is not private, just require read capability
                $primitive_caps[] = $post_type->cap->read;
            } elseif ($post->post_author == $user_id) {
                // Post is private, but current user is author
                $primitive_caps[] = $post_type->cap->read;
            } else {
                // Post is private, and current user is not the author
                $primitive_caps[] = $post_type->cap->read_private_post;
            }
            break;
        case ‘delete_download_item':
            if ($post->post_author == $user_id) {
                // Current user is author, require delete_download_items capability
                $primitive_caps[] = $post_type->cap->delete_posts;
            } else {
                // Current user is no the author, require delete_others_download_items capability
                $primitive_caps[] = $post_type->cap->delete_others_posts;
            }
            // If post is published, require delete_published_posts capability too
            if (‘publish’ == $post->post_status) {
                $primitive_caps[] = $post_type->cap->delete_published_posts;
            }
            break;
    endswitch;
    return $primitive_caps;
}
function crud_frontendDeleteLink() {
    if (!current_user_can(‘delete_download_items’)) {
        echo ‘    ‘;
        return;
    }
    $url = add_query_arg(
            array(
                ‘action’ => ‘frontend_delete’,
                ‘post’ => get_the_ID()
            )
    );
    echo “Delete“;
}
if (isset($_REQUEST[‘action’]) && $_REQUEST[‘action’] == ‘frontend_delete’) {
    add_action(‘init’, ‘crud_FrontendDeletePost’);
}
function crud_FrontendDeletePost() {
    if (!current_user_can(‘delete_download_items’))
        return;
    $post_id = (isset($_REQUEST[‘post’]) ? (int) $_REQUEST[‘post’] : 0);
    if (empty($post_id))
        return;
    wp_trash_post($post_id);
}
add_shortcode(‘downloadItem’, crud_downloadItemFun);
function crud_downloadItemFun() {
    set_caps();
    global $post ;
   $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $args = array(
      ‘posts_per_page’ => 10,
      ‘post_type’ => ‘download_items’,
      ‘paged’ => $paged
    );
    $newsObj = new WP_Query($args);
    if ($newsObj->have_posts()):    ?>
       

           
               

               

               

               

               

           

           
                while ($newsObj->have_posts()): $newsObj->the_post();
        $curr_id = get_the_ID();
        $meta = get_post_meta( $curr_id,’crud_category’ );
        $meta = preg_replace(‘/[^a-zA-Z0-9\,]/’, ”, $meta);
        $download_price = get_post_meta( $curr_id,’download_price’ );
        ?>
           

               

               

               

               

               

           

                endwhile;?>
       

Title Categories Price Edit Delete
0 ) {echo $meta[0] ;} ?> 0 ) echo implode(‘,’, $download_price); ?> “>Edit

            endif;
    wp_reset_query();
}
add_action(‘admin_menu’,’crud_pluginSetting’);
function crud_pluginSetting() {
    add_menu_page(‘Crud Plugin Settings’, ‘Crud Settings’, ‘administrator’, ‘crud_settings’, ‘crud_settingFunction’);
}
if (isset($_REQUEST[‘category_label’]) && (!empty ($_REQUEST[‘category_label’]) || ” != ‘frontend_delete’)) {
    crud_installData($_REQUEST[‘category_name’], $_REQUEST[‘category_label’]);
}
function crud_settingFunction() {
    $category_label = (get_option(‘category_label’) != ”) ? get_option(‘category_label’) : ‘category_label100′;
    $category_name = (get_option(‘category_name’) != ”) ? get_option(‘category_name’) : ‘category_name100′;
    $html = ‘

       

       

Select Your Settings

        ‘ . wp_nonce_field(‘update-options’) . ‘
           

           
               
                   

               

               

                   

               

           

           

                       
                   
                       
                   

           
 

           

';
    echo $html;
}
function crud_createDBTable() {
    global $wpdb;
    $table_name = $wpdb->prefix . "categories";
    $sql = "CREATE TABLE $table_name (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  category_name text NOT NULL,
  category_label text NOT NULL,
  UNIQUE KEY id (id)
    );";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta($sql);
}
function crud_dropTable() {
    global $wpdb;
    $table_name = $wpdb->prefix . "categories";
    $sql = "DROP TABLE $table_name";
}
function crud_installData($category_name = "News",$category_Label = "News") {
   global $wpdb;
   $table_name = $wpdb->prefix . "categories";
   //$category_name = "News";
   //$category_Label = "News";
   $rows_affected = $wpdb->insert( $table_name, array( 'category_name' => $category_name, 'category_Label' => $category_Label) );
}
//Debug Errors if there are any
add_action('activated_plugin','save_error');
function save_error(){
    file_put_contents(ABSPATH. 'wp-content/uploads/2013/error_activation.html', ob_get_contents());
}
?>