Patch: Some improvements
-
Hello,
Sorry if this is not the right place, but I would like to offer a small patch for the current code in the trunk:
https://fileshare.steffenl.com/f/a37dd2545b/
Note: Code quality in the patch could be better, so feel free to make changes.
Changes:
- Fixed typo in UI.
- Use wp_enqueue_*() for scripts and stylesheets.
- Enforce login when viewing or creating a ticket by redirecting to the login page rather than displaying the login form on the current page.
Let’s see if it works alright to paste the whole patch here:
Index: admin/ost-config.php =================================================================== --- admin/ost-config.php (revision 968450) +++ admin/ost-config.php (working copy) @@ -121,4 +121,4 @@ </div> </form> </div><!--End of wrap--> -<script language="javascript" src="<?php echo plugin_dir_url(__FILE__).'../js/fade.js';?>"></script> \ No newline at end of file +<?php wp_enqueue_script('ost-bridge-fade', plugin_dir_url(__FILE__).'../js/fade.js');?> \ No newline at end of file Index: admin/ost-ticketview.php =================================================================== --- admin/ost-ticketview.php (revision 968450) +++ admin/ost-ticketview.php (working copy) @@ -141,4 +141,4 @@ </tr> </table> </div><!--End wrap--> -<script language="javascript" src="<?php echo plugin_dir_url(__FILE__).'../js/fade.js';?>"></script> \ No newline at end of file +<?php wp_enqueue_script('ost-bridge-fade', plugin_dir_url(__FILE__).'../js/fade.js');?> \ No newline at end of file Index: ost-bridge.php =================================================================== --- ost-bridge.php (revision 968450) +++ ost-bridge.php (working copy) @@ -41,7 +41,8 @@ require_once( WP_PLUGIN_DIR . '/key4ce-osticket-bridge/osticket-wp.php'); } -add_shortcode('addosticket', 'addtemplate'); +define('OST_SHORTCODE_ADDOSTICKET', 'addosticket'); +add_shortcode(OST_SHORTCODE_ADDOSTICKET, 'addtemplate'); function custom_toolbar_openticket() { global $wp_admin_bar; $wp_admin_bar->add_menu(array( @@ -127,7 +128,7 @@ } } function mb_admin_css() { -echo '<link rel="stylesheet" type="text/css" media="all" href="'.plugin_dir_url(__FILE__).'css/admin-style.css">'; +wp_enqueue_style('ost-bridge-admin', plugin_dir_url(__FILE__).'css/admin-style.css">'); } function mb_install() @@ -285,4 +286,34 @@ } } } + +// Begin: Workaround when redirecting from shortcodes. +// Because we potentially need to redirect within a shortcode, we must make sure that headers are not sent beforehand. +// Ref.: https://stackoverflow.com/questions/17120634/how-to-require-a-login-on-a-wordpress-page-with-a-shortcode/17124281#17124281 + +// Looks for a shortcode within the current post's content. +// Optimized for shortcodes that don't have parameters. +function ost_has_shortcode_without_params($shortcode = '') { + global $post; + + if (!$shortcode || $post == null) { + return false; + } + + if (stripos($post->post_content, '[' . $shortcode . ']') === false) { + return false; + } + + return true; +} + +// User must be logged in to view pages that use the shortcode +function ost_enforce_login_action() { + if(ost_has_shortcode_without_params(OST_SHORTCODE_ADDOSTICKET) && !is_user_logged_in()) { + auth_redirect(); + } +} + +add_action('wp', 'ost_enforce_login_action'); +// End: Workaround when redirecting from shortcodes. ?> Index: osticket-wp.php =================================================================== --- osticket-wp.php (revision 968450) +++ osticket-wp.php (working copy) @@ -5,8 +5,8 @@ if(is_user_logged_in()) { $config = get_option('os_ticket_config'); extract($config); -?> -<link rel="stylesheet" type="text/css" media="all" href="<?php echo plugin_dir_url(__FILE__).'css/style.css'; ?>" /> + +wp_enqueue_style('ost-bridge', plugin_dir_url(__FILE__).'css/style.css'); ?> <div id="ost_container"><!--ost_container Start--> <?php require_once( WP_PLUGIN_DIR . '/key4ce-osticket-bridge/includes/functions.php' ); ?> <?php @@ -108,23 +108,5 @@ </div><!--ost_container End--> <?php } else { - $Login_args = array( - 'echo' => true, - 'redirect' => site_url( $_SERVER['REQUEST_URI'] ), - 'form_id' => 'loginform', - 'label_username' => __( 'Username' ), - 'label_password' => __( 'Password' ), - 'label_remember' => __( 'Remember Me' ), - 'label_log_in' => __( 'Log In' ), - 'id_username' => 'user_login', - 'id_password' => 'user_pass', - 'id_remember' => 'rememberme', - 'id_submit' => 'wp-submit', - 'remember' => true, - 'value_username' => NULL, - 'value_remember' => false -); -?><div><br /><br /> <h3>Sorry, you must first log in to view your tickets. If you do not have a account yet you can <a style="color: #2991D6;" href="<?php echo wp_registration_url(); ?>">register here</a>. </h3> -<br /><br /> -<?php wp_login_form(@$login_args); ?> -</div><?php } ?> \ No newline at end of file +throw new Exception('Should not happen: User is not logged in'); +} ?> Index: templates/nav_bar.php =================================================================== --- templates/nav_bar.php (revision 968450) +++ templates/nav_bar.php (working copy) @@ -52,5 +52,5 @@ echo '<div style="clear: both"></div>'; echo '</div>'; echo '</div><hr style="border-color:#D5E5EE; border-width: 3px; height: 3px;">'; -?> -<script type="text/javascript" src="<?php echo plugin_dir_url(__FILE__).'../js/validate.js';?>"></script> \ No newline at end of file + +wp_enqueue_script('ost-bridge-validate', plugin_dir_url(__FILE__).'../js/validate.js'); \ No newline at end of file Index: templates/new_ticket.php =================================================================== --- templates/new_ticket.php (revision 968450) +++ templates/new_ticket.php (working copy) @@ -47,7 +47,7 @@ <div id="new_ticket_subject">Subject:</div> <div id="new_ticket_subject_input"><input class="ost" id="subject" type="text" name="subject" size="35"/><font class="error">?*</font></div> <div style="clear: both"></div> -<div id="new_ticket_catagory">Catagories:</div> +<div id="new_ticket_catagory">Categories:</div> <div id="new_ticket_catagory_input"> <select id="deptId" name="deptId"> <option value="" selected="selected"> Select a Category </option>
Viewing 13 replies - 1 through 13 (of 13 total)
Viewing 13 replies - 1 through 13 (of 13 total)
- The topic ‘Patch: Some improvements’ is closed to new replies.