function wfu_ajax_action_edit_shortcode() { if (!current_user_can('manage_options')) { die; } if (!isset($_POST['upload_id']) || !isset($_POST['post_id']) || !isset($_POST['post_hash']) || !isset($_POST['shortcode_tag'])) { die; } //sanitize parameters $upload_id = sanitize_text_field($_POST['upload_id']); $post_id = wfu_sanitize_int($_POST['post_id']); $post_hash = wfu_sanitize_code($_POST['post_hash']); $shortcode_tag = wfu_sanitize_tag($_POST['shortcode_tag']); $data['post_id'] = $post_id; $data['post_hash'] = $post_hash; if (wfu_check_edit_shortcode($data)) { $post = get_post($data['post_id']); //get default value for uploadid $defs = wfu_attribute_definitions(); $default = ""; foreach ($defs as $key => $def) { if ($def['attribute'] == 'uploadid') { $default = $def['value']; break; } } //get page shortcodes $wfu_shortcodes = wfu_get_content_shortcodes($post, $shortcode_tag); //find the shortcodes' uploadid and the correct one $validkey = -1; foreach ($wfu_shortcodes as $key => $data) { $shortcode = trim(substr($data['shortcode'], strlen('[' . $shortcode_tag), -1)); $shortcode_attrs = wfu_shortcode_string_to_array($shortcode); if (array_key_exists('uploadid', $shortcode_attrs)) { $uploadid = $shortcode_attrs['uploadid']; } else { $uploadid = $default; } if ($uploadid == $upload_id) { $validkey = $key; break; } } if ($validkey == -1) { die; } $data_enc = wfu_safe_store_shortcode_data(wfu_encode_array_to_string($wfu_shortcodes[$validkey])); $url = site_url() . '/wp-admin/options-general.php?page=wordpress_file_upload&tag=' . $shortcode_tag . '&action=edit_shortcode&data=' . $data_enc; die("wfu_edit_shortcode:success:" . wfu_plugin_encode_string($url)); } else { die("wfu_edit_shortcode:check_page_obsolete:" . WFU_ERROR_PAGE_OBSOLETE); } }
function wfu_manage_instances_of_shortcode($tag, $title, $slug, $inc) { global $wp_registered_widgets, $wp_registered_sidebars; $siteurl = site_url(); $args = array('post_type' => array("post", "page"), 'post_status' => "publish,private,draft", 'posts_per_page' => -1); $posts = get_posts($args); $wfu_shortcodes = array(); //get shortcode instances from page/posts foreach ($posts as $post) { $ret = wfu_get_content_shortcodes($post, $tag); if ($ret !== false) { $wfu_shortcodes = array_merge($wfu_shortcodes, $ret); } } //get shortcode instances from sidebars $data = array(); $widget_base = $tag . '_widget'; if (is_array($wp_registered_widgets)) { foreach ($wp_registered_widgets as $id => $widget) { if (substr($id, 0, strlen($widget_base)) == $widget_base) { $widget_obj = isset($widget['callback']) ? isset($widget['callback'][0]) ? $widget['callback'][0] instanceof WP_Widget ? $widget['callback'][0] : false : false : false; $widget_sidebar = is_active_widget(false, $id, $widget_base); if ($widget_obj !== false && $widget_sidebar !== false) { if (isset($wp_registered_sidebars[$widget_sidebar]) && isset($wp_registered_sidebars[$widget_sidebar]['name'])) { $widget_sidebar = $wp_registered_sidebars[$widget_sidebar]['name']; } $data['post_id'] = ""; $data['post_hash'] = ""; $data['shortcode'] = $widget_obj->shortcode(); $data['position'] = 0; $data['widgetid'] = $id; $data['sidebar'] = $widget_sidebar; array_push($wfu_shortcodes, $data); } } } } $list = wfu_construct_post_list($posts); $pagelist = wfu_flatten_post_list($list["page"]); $postlist = wfu_flatten_post_list($list["post"]); $echo_str = "\n\t\t" . '<h3 style="margin-bottom: 10px; margin-top: 40px;">' . $title . '</h3>'; $onchange_js = 'document.getElementById(\'wfu_add_plugin_ok_' . $inc . '\').disabled = !((document.getElementById(\'wfu_page_type_' . $inc . '\').value == \'page\' && document.getElementById(\'wfu_page_list_' . $inc . '\').value != \'\') || (document.getElementById(\'wfu_page_type_' . $inc . '\').value == \'post\' && document.getElementById(\'wfu_post_list_' . $inc . '\').value != \'\'));'; $no_shortcodes = count($wfu_shortcodes) == 0; $echo_str .= "\n\t\t" . '<div id="wfu_add_plugin_button_' . $inc . '" style="' . (!$no_shortcodes ? '' : 'color:blue; font-weight:bold; font-size:larger;') . 'margin-bottom: 20px; margin-top: 10px;">'; $addbutton_pre = !$no_shortcodes ? '' : '<label>Press </label>'; $addbutton_post = !$no_shortcodes ? '' : '<label> to get started and add the ' . $slug . ' in a page</label>'; $echo_str .= "\n\t\t\t" . $addbutton_pre . '<button onclick="document.getElementById(\'wfu_add_plugin_button_' . $inc . '\').style.display = \'none\'; document.getElementById(\'wfu_add_plugin_' . $inc . '\').style.display = \'inline-block\'; ' . $onchange_js . '">' . (!$no_shortcodes ? 'Add Plugin Instance' : 'here') . '</button>' . $addbutton_post; $echo_str .= "\n\t\t" . '</div>'; $echo_str .= "\n\t\t" . '<div id="wfu_add_plugin_' . $inc . '" style="margin-bottom: 20px; margin-top: 10px; position:relative; display:none;">'; $echo_str .= "\n\t\t\t" . '<div id="wfu_add_plugin_' . $inc . '_overlay" style="position:absolute; top:0; left:0; width:100%; height:100%; background-color:rgba(255,255,255,0.8); border:none; display:none;">'; $echo_str .= "\n\t\t\t\t" . '<table style="background:none; border:none; margin:0; padding:0; line-height:1; border-spacing:0; width:100%; height:100%; table-layout:fixed;"><tbody><tr><td style="text-align:center; vertical-align:middle;"><div style="display:inline-block;"><span class="spinner" style="opacity:1; float:left; margin:0; display:inline;"></span><label style="margin-left:4px;">please wait...</label></div></td></tr></tbody></table>'; $echo_str .= "\n\t\t\t" . '</div>'; $echo_str .= "\n\t\t\t" . '<label>Add ' . $slug . ' to </label><select id="wfu_page_type_' . $inc . '" onchange="document.getElementById(\'wfu_page_list_' . $inc . '\').style.display = (this.value == \'page\' ? \'inline-block\' : \'none\'); document.getElementById(\'wfu_post_list_' . $inc . '\').style.display = (this.value == \'post\' ? \'inline-block\' : \'none\'); ' . $onchange_js . '"><option value="page" selected="selected">Page</option><option value="post">Post</option></select>'; $echo_str .= "\n\t\t\t" . '<select id="wfu_page_list_' . $inc . '" style="margin-bottom:6px;" onchange="' . $onchange_js . '">'; $echo_str .= "\n\t\t\t\t" . '<option value=""></option>'; foreach ($pagelist as $item) { $echo_str .= "\n\t\t\t\t" . '<option value="' . $item['id'] . '">' . str_repeat(' ', 4 * $item['level']) . ($item['status'] == 1 ? '[Private]' : ($item['status'] == 2 ? '[Draft]' : '')) . $item['title'] . '</option>'; } $echo_str .= "\n\t\t\t" . '</select>'; $echo_str .= "\n\t\t\t" . '<select id="wfu_post_list_' . $inc . '" style="display:none; margin-bottom:6px;" onchange="' . $onchange_js . '">'; $echo_str .= "\n\t\t\t\t" . '<option value=""></option>'; foreach ($postlist as $item) { $echo_str .= "\n\t\t\t\t" . '<option value="' . $item['id'] . '">' . str_repeat(' ', 4 * $item['level']) . ($item['status'] == 1 ? '[Private]' : ($item['status'] == 2 ? '[Draft]' : '')) . $item['title'] . '</option>'; } $echo_str .= "\n\t\t\t" . '</select><br />'; $add_shortcode_ticket = wfu_create_random_string(16); $_SESSION['wfu_add_shortcode_ticket_for_' . $tag] = $add_shortcode_ticket; $echo_str .= "\n\t\t" . '<button id="wfu_add_plugin_ok_' . $inc . '" style="float:right; margin: 0 2px 0 4px;" disabled="disabled" onclick="document.getElementById(\'wfu_add_plugin_' . $inc . '_overlay\').style.display = \'block\'; window.location = \'' . $siteurl . '/wp-admin/options-general.php?page=wordpress_file_upload&action=add_shortcode&tag=' . $tag . '&postid=\' + (document.getElementById(\'wfu_page_type_' . $inc . '\').value == \'page\' ? document.getElementById(\'wfu_page_list_' . $inc . '\').value : document.getElementById(\'wfu_post_list_' . $inc . '\').value) + \'&nonce=' . $add_shortcode_ticket . '\';">Ok</button>'; $echo_str .= "\n\t\t" . '<button style="float:right;" onclick="document.getElementById(\'wfu_page_type_' . $inc . '\').value = \'page\'; document.getElementById(\'wfu_page_list_' . $inc . '\').value = \'\'; document.getElementById(\'wfu_post_list_' . $inc . '\').value = \'\'; document.getElementById(\'wfu_add_plugin_' . $inc . '\').style.display = \'none\'; document.getElementById(\'wfu_add_plugin_button_' . $inc . '\').style.display = \'inline-block\';">Cancel</button>'; $echo_str .= "\n\t\t" . '</div>'; $echo_str .= "\n\t\t" . '<table class="wp-list-table widefat fixed striped">'; $echo_str .= "\n\t\t\t" . '<thead>'; $echo_str .= "\n\t\t\t\t" . '<tr>'; $echo_str .= "\n\t\t\t\t\t" . '<th scope="col" width="5%" style="text-align:center;">'; $echo_str .= "\n\t\t\t\t\t\t" . '<label>#</label>'; $echo_str .= "\n\t\t\t\t\t" . '</th>'; // $echo_str .= "\n\t\t\t\t\t".'<th scope="col" width="10%" style="text-align:center;">'; // $echo_str .= "\n\t\t\t\t\t\t".'<label>ID</label>'; // $echo_str .= "\n\t\t\t\t\t".'</th>'; $echo_str .= "\n\t\t\t\t\t" . '<th scope="col" width="10%" style="text-align:center;">'; $echo_str .= "\n\t\t\t\t\t\t" . '<label>Type</label>'; $echo_str .= "\n\t\t\t\t\t" . '</th>'; $echo_str .= "\n\t\t\t\t\t" . '<th scope="col" width="30%" style="text-align:center;">'; $echo_str .= "\n\t\t\t\t\t\t" . '<label>Title</label>'; $echo_str .= "\n\t\t\t\t\t" . '</th>'; $echo_str .= "\n\t\t\t\t\t" . '<th scope="col" width="45%" style="text-align:center;">'; $echo_str .= "\n\t\t\t\t\t\t" . '<label>Shortcode</label>'; $echo_str .= "\n\t\t\t\t\t" . '</th>'; $echo_str .= "\n\t\t\t\t" . '</tr>'; $echo_str .= "\n\t\t\t" . '</thead>'; $echo_str .= "\n\t\t\t" . '<tbody>'; $i = 1; foreach ($wfu_shortcodes as $key => $data) { $widget_id = isset($data['widgetid']) ? $data['widgetid'] : ''; if ($widget_id == "") { $id = $data['post_id']; $posttype_obj = get_post_type_object(get_post_type($id)); $type = $posttype_obj ? $posttype_obj->labels->singular_name : ""; $title = get_the_title($id); if (trim($title) == "") { $title = 'ID: ' . $id; } } else { $type = 'Sidebar'; $title = $data['sidebar']; } $data_enc = wfu_safe_store_shortcode_data(wfu_encode_array_to_string($data)); $echo_str .= "\n\t\t\t\t" . '<tr onmouseover="var actions=document.getElementsByName(\'wfu_shortcode_actions_' . $inc . '\'); for (var i=0; i<actions.length; i++) {actions[i].style.visibility=\'hidden\';} document.getElementById(\'wfu_shortcode_actions_' . $inc . '_' . $i . '\').style.visibility=\'visible\'" onmouseout="var actions=document.getElementsByName(\'wfu_shortcode_actions_' . $inc . '\'); for (var i=0; i<actions.length; i++) {actions[i].style.visibility=\'hidden\';}">'; $echo_str .= "\n\t\t\t\t\t" . '<td style="padding: 5px 5px 5px 10px; text-align:center;">'; $echo_str .= "\n\t\t\t\t\t\t" . '<a class="row-title" href="' . $siteurl . '/wp-admin/options-general.php?page=wordpress_file_upload&action=edit_shortcode&tag=' . $tag . '&data=' . $data_enc . '" title="Instance #' . $i . '">Instance ' . $i . '</a>'; $echo_str .= "\n\t\t\t\t\t\t" . '<div id="wfu_shortcode_actions_' . $inc . '_' . $i . '" name="wfu_shortcode_actions_' . $inc . '" style="visibility:hidden;">'; $echo_str .= "\n\t\t\t\t\t\t\t" . '<span>'; $echo_str .= "\n\t\t\t\t\t\t\t\t" . '<a href="' . $siteurl . '/wp-admin/options-general.php?page=wordpress_file_upload&action=edit_shortcode&tag=' . $tag . '&data=' . $data_enc . '" title="Edit this shortcode">Edit</a>'; $echo_str .= "\n\t\t\t\t\t\t\t\t" . ' | '; $echo_str .= "\n\t\t\t\t\t\t\t" . '</span>'; $echo_str .= "\n\t\t\t\t\t\t\t" . '<span>'; $echo_str .= "\n\t\t\t\t\t\t\t\t" . '<a href="' . $siteurl . '/wp-admin/options-general.php?page=wordpress_file_upload&action=delete_shortcode&data=' . $data_enc . '" title="Delete this shortcode">Delete</a>'; $echo_str .= "\n\t\t\t\t\t\t\t" . '</span>'; $echo_str .= "\n\t\t\t\t\t\t" . '</div>'; $echo_str .= "\n\t\t\t\t\t" . '</td>'; // $echo_str .= "\n\t\t\t\t\t".'<td style="padding: 5px 5px 5px 10px; text-align:center;">'.$id.'</td>'; $echo_str .= "\n\t\t\t\t\t" . '<td style="padding: 5px 5px 5px 10px; text-align:center;">' . $type . '</td>'; $echo_str .= "\n\t\t\t\t\t" . '<td style="padding: 5px 5px 5px 10px; text-align:center;">' . $title . '</td>'; $echo_str .= "\n\t\t\t\t\t" . '<td style="padding: 5px 5px 5px 10px; text-align:left;">'; $echo_str .= "\n\t\t\t\t\t\t" . '<textarea rows="3" disabled="disabled" style="width:100%;">' . trim($data['shortcode']) . '</textarea>'; $echo_str .= "\n\t\t\t\t\t" . '</td>'; $echo_str .= "\n\t\t\t\t" . '</tr>'; $i++; } $echo_str .= "\n\t\t\t" . '</tbody>'; $echo_str .= "\n\t\t" . '</table>'; return $echo_str; }