Plugin Directory

source: bulk-edit-post-titles/trunk/classes/class.bulk.titles.php

Last change on this file was 2177326, checked in by pawaryogesh1989, 5 years ago

Added Configuration Menu to manage bulk edit settings.

File size: 6.3 KB
Line 
1<?php
2
3class Custom_Bulk_Edit_Title
4{
5
6    /**
7     * Constructor of the class
8     * Author : Yogesh Pawar
9     * Date : 5th Feb 2019
10     */
11    function __construct()
12    {
13        $this->initBulkAction();
14
15        add_action('wp_ajax_bulk_update_post_titles', array(&$this, 'bulkUpdatePostTitles'));
16        add_action('wp_ajax_edit_bulk_titles', array(&$this, 'editBulkTitles'));
17        add_action('admin_init', array(&$this, 'addBulkEditScripts'));
18        add_action('admin_menu', array($this, 'bulkEditMenu'));
19        add_action('wp_ajax_bulkAddRemovePostTypes', array($this, 'bulkAddRemovePostTypes'));
20    }
21
22    /**
23     * Function to init the bulk action
24     * Author : Yogesh Pawar
25     * Date : 5th Feb 2019
26     */
27    function initBulkAction()
28    {
29        if (is_admin()) {
30            add_action('admin_footer-edit.php', array(&$this, 'bulkCustomAdminFooter'));
31            add_action('admin_notices', array(&$this, 'bulkEditSuccess'));
32        }
33    }
34
35    /**
36     * Function to Load Settings Page
37     */
38    function bulkEditMenu()
39    {
40
41        add_management_page('Bulk Edit Settings', 'Bulk Edit Settings', 'manage_options', 'bulk-edit-settings', array($this, 'loadBulkEditSettings'), '', 86);
42    }
43
44    /**
45     * function to load settings Page
46     */
47    function loadBulkEditSettings()
48    {
49        if (current_user_can('manage_options')) {
50            if (file_exists(plugin_dir_path(__DIR__) . '/views/bulk-edit-options.php')) {
51                require plugin_dir_path(__DIR__) . '/views/bulk-edit-options.php';
52            } else {
53                die('<br /><h3>Plugin Installation is Incomplete. Please install the plugin again or make sure you have copied all the plugin files.</h3>');
54            }
55        } else {
56            wp_die(__('You do not have sufficient permissions to access this page.'));
57        }
58    }
59
60    /**
61     * Function to enqueue scripts
62     * Author : Yogesh Pawar
63     * Date : 6th Feb 2019
64     */
65    function addBulkEditScripts()
66    {
67        if (is_user_logged_in() && is_admin()) {
68
69            $current_page = filter_input(INPUT_GET, 'page');
70
71            if ($current_page == "bulk-edit-settings") {
72                wp_register_style('bulk-bootstrap', plugins_url('/assets/css/bootstrap.min.css', __DIR__));
73                wp_register_style('bulk-backend-css', plugins_url('/assets/css/bulk-multi-select.css', __DIR__));
74                wp_enqueue_style('bulk-bootstrap');
75                wp_enqueue_style('bulk-backend-css');
76
77                wp_register_script('bulk-multi-js', plugins_url('/assets/js/jquery.multi-select.js', __DIR__));
78                wp_enqueue_script('bulk-multi-js');
79            }
80
81            wp_enqueue_style('bulk-action-style', plugins_url('assets/css/bulk-style.css', __DIR__));
82            wp_enqueue_script('bulk-action-script', plugins_url('/assets//js/bulk-action.js', __DIR__), array(), '1.0.0', true);
83
84            wp_localize_script('bulk-action-script', 'bulkactionscript', array(
85                'pluginsUrl' => BULK_FILE_DIRECTORY,
86                'site_url' => site_url(),
87                'bulk_ajax_url' => admin_url('admin-ajax.php')
88            ));
89
90            add_thickbox();
91        }
92    }
93
94    /**
95     * Function to append the bulk edit title
96     * @global type $post_type
97     * Author: Yogesh Pawar
98     * Date : 6th Feb 2019
99     */
100    function bulkCustomAdminFooter()
101    {
102        global $post_type;
103
104        $postTypes = get_option("bulk_edit_posts");
105
106        if (!empty($postTypes)) {
107            $postTypeList = explode(",", $postTypes);
108        } else {
109            $postTypeList = array('post', 'page');
110        }
111
112        if (in_array($post_type, $postTypeList)) {
113
114            ?>
115            <script type="text/javascript">
116                jQuery(document).ready(function () {
117                    jQuery('<option>').val('bulk-edit-title').text('Edit Title').appendTo("select[name='action']");
118                    jQuery('<option>').val('bulk-edit-title').text('Edit Title').appendTo("select[name='action2']");
119                });
120            </script>
121            <?php
122        }
123    }
124
125    /**
126     * Function which bulk updates the post titles
127     * Author : Yogesh Pawar
128     * Date : 5th Feb 2019
129     */
130    function bulkUpdatePostTitles()
131    {
132        $post_bulk_titles = ($_REQUEST['post_title']);
133        $post_bulk_id = ($_REQUEST['post_id']);
134
135        for ($i = 0; $i < sizeof($post_bulk_id); $i++) {
136
137            if ($post_bulk_titles[$i] != "") {
138
139                $bulk_post = array(
140                    'ID' => intval($post_bulk_id[$i]),
141                    'post_title' => sanitize_text_field($post_bulk_titles[$i]),
142                );
143                wp_update_post($bulk_post);
144            }
145        }
146        echo "done";
147        exit;
148    }
149
150    /**
151     * Function to display the sucess the message
152     * Author : Yogesh Pawar
153     * Date : 6th Feb 2019
154     */
155    function bulkEditSuccess()
156    {
157        if (!empty(($_REQUEST["method"]))) {
158            if (esc_attr($_REQUEST['method']) == "success") {
159
160                ?>
161                <div class="notice notice-success is-dismissible">
162                    <p><?php _e('<b>Titles Updated Sucessfully!</b>', 'sample-text-domain'); ?></p>
163                </div>
164                <?php
165            }
166        }
167    }
168
169    /**
170     * Function to load the bulk edit template
171     * Author : Yogesh Pawar
172     * Date : 5th Feb 2019
173     */
174    function editBulkTitles()
175    {
176        require(plugin_dir_path(__DIR__) . "views/edit-bulk-titles.php");
177        exit;
178    }
179
180    /**
181     * Function to update posts for which user wants to enable bulk edit of titles
182     */
183    function bulkAddRemovePostTypes()
184    {
185
186        if (empty($_POST['bulk_edit_posts'])) {
187            echo json_encode(array('status' => 'failure', 'message' => 'Please select atleast one Post type'));
188            exit();
189        } else {
190            $allowed_posts = implode(",", $_POST['bulk_edit_posts']);
191            update_option("bulk_edit_posts", $allowed_posts);
192
193            echo json_encode(array('status' => 'success', 'message' => 'Bulk Edit Permissions updated for selected Post Types!'));
194            exit();
195        }
196    }
197}
198
199new Custom_Bulk_Edit_Title();
200
201?>
Note: See TracBrowser for help on using the repository browser.