From d666a668a9f3572014cb8befd9234eaa3dbb197d Mon Sep 17 00:00:00 2001 From: Ozh Date: Fri, 8 Feb 2013 17:37:26 +0000 Subject: [PATCH] ZOMG, big commit. - Fixed the sorting and pagination, gave variables much more meaningful names, reduced variables used - functions-html.php is now i10n ready - Added basic phpdoc to functions-html.php while I was at it git-svn-id: http://yourls.googlecode.com/svn/trunk@853 12232710-3e20-11de-b438-597f59cd7555 --- admin/index.php | 205 ++++++++++++++-------------- includes/functions-html.php | 263 +++++++++++++++++++++++------------- includes/functions.php | 15 +- 3 files changed, 293 insertions(+), 190 deletions(-) diff --git a/admin/index.php b/admin/index.php index 6c563fb..c655a38 100644 --- a/admin/index.php +++ b/admin/index.php @@ -5,120 +5,120 @@ // Variables $table_url = YOURLS_DB_TABLE_URL; +$where = $search_sentence = $search_text = $url = $keyword = ''; +$date_filter = $date_first = $date_second = ''; +$base_page = yourls_admin_url( 'index.php' ); + // Default SQL behavior -$where = $search_display = $search_text = $search_url = $url = $keyword = ''; -$search_in_text = 'URL'; -$search_in_sql = 'url'; -$sort_by_text = 'Short URL'; -$sort_by_sql = 'timestamp'; -$sort_order_text = 'Descending Order'; -$sort_order_sql = 'desc'; -$page = ( isset( $_GET['page'] ) ? intval($_GET['page']) : 1 ); -$search = ( isset( $_GET['s_search'] ) ? htmlspecialchars( trim($_GET['s_search']) ) : '' ); -$perpage = ( isset( $_GET['perpage'] ) && intval( $_GET['perpage'] ) ? intval($_GET['perpage']) : 15 ); -$link_limit = ( isset( $_GET['link_limit'] ) && !empty( $_GET['link_limit'] ) ) ? intval($_GET['link_limit']) : '' ; -if ( $link_limit !== '' ) { - $link_filter = ( isset( $_GET['link_filter'] ) && $_GET['link_filter'] == 'more' ? 'more' : 'less' ) ; - $link_moreless = ( $link_filter == 'more' ? '>' : '<' ); - $where = " AND clicks $link_moreless $link_limit"; +$search_in_text = yourls__( 'URL' ); +$search_in = 'url'; +$sort_by_text = yourls__( 'Short URL' ); +$sort_by = 'timestamp'; +$sort_order_text = yourls__( 'Descending Order' ); +$sort_order = 'desc'; +$page = ( isset( $_GET['page'] ) ? intval($_GET['page']) : 1 ); +$search = ( isset( $_GET['search'] ) ? htmlspecialchars( trim($_GET['search']) ) : '' ); +$perpage = ( isset( $_GET['perpage'] ) && intval( $_GET['perpage'] ) ? intval($_GET['perpage']) : 15 ); +$click_limit = ( isset( $_GET['click_limit'] ) && $_GET['click_limit'] !== '' ) ? intval( $_GET['click_limit'] ) : '' ; +if ( $click_limit !== '' ) { + $click_filter = ( isset( $_GET['click_filter'] ) && $_GET['click_filter'] == 'more' ? 'more' : 'less' ) ; + $click_moreless = ( $click_filter == 'more' ? '>' : '<' ); + $where = " AND clicks $click_moreless $click_limit"; } else { - $link_filter = ''; + $click_filter = ''; } -$date_filter = 'before'; -$date_first = $date_second = ''; -$base_page = yourls_admin_url( 'index.php' ); // Searching -if( !empty($search) && !empty($_GET['s_in']) ) { - switch($_GET['s_in']) { +if( !empty( $search ) && !empty( $_GET['search_in'] ) ) { + switch( $_GET['search_in'] ) { case 'keyword': - $search_in_text = 'Short URL'; - $search_in_sql = 'keyword'; + $search_in_text = yourls__( 'Short URL' ); + $search_in = 'keyword'; break; case 'url': - $search_in_text = 'URL'; - $search_in_sql = 'url'; + $search_in_text = yourls__( 'URL' ); + $search_in = 'url'; break; case 'title': - $search_in_text = 'Title'; - $search_in_sql = 'title'; + $search_in_text = yourls__( 'Title' ); + $search_in = 'title'; break; case 'ip': - $search_in_text = 'IP Address'; - $search_in_sql = 'ip'; + $search_in_text = yourls__( 'IP Address' ); + $search_in = 'ip'; break; } - $search_text = stripslashes($search); - $search_display = "Searching for $search_text in $search_in_text. "; - $search_url = "&s_search=$search_text &s_in=$search_in_sql"; - $search = str_replace('*', '%', '*'.$search.'*'); - $where .= " AND `$search_in_sql` LIKE ('$search')"; + $search_sentence = sprintf( yourls__( 'Searching for %s in %s.' ), yourls_esc_html( $search ), yourls_esc_html( $search_in_text ) ); + $search_url = yourls_sanitize_url( "&search=$search&search_in=$search_in" ); + $search_text = $search; + $search = str_replace( '*', '%', '*' . yourls_escape( $search ) . '*' ); + $where .= " AND `$search_in` LIKE ('$search')"; } // Time span -if( !empty($_GET['date_filter']) ) { - switch($_GET['date_filter']) { +if( !empty( $_GET['date_filter'] ) ) { + switch( $_GET['date_filter'] ) { case 'before': $date_filter = 'before'; - if( yourls_sanitize_date( $_GET['date_first'] ) ) { + if( isset( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_first'] ) ) { + $date_first = yourls_sanitize_date( $_GET['date_first'] ); $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] ); $where .= " AND `timestamp` < '$date_first_sql'"; - $date_first = $_GET['date_first']; } break; case 'after': $date_filter = 'after'; - if( yourls_sanitize_date( $_GET['date_first'] ) ) { + if( isset( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_first'] ) ) { $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] ); + $date_first = yourls_sanitize_date( $_GET['date_first'] ); $where .= " AND `timestamp` > '$date_first_sql'"; - $date_first = $_GET['date_first']; } break; case 'between': $date_filter = 'between'; - if( yourls_sanitize_date( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_second'] ) ) { - $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] ); + if( isset( $_GET['date_first'] ) && isset( $_GET['date_second'] ) && yourls_sanitize_date( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_second'] ) ) { + $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] ); $date_second_sql = yourls_sanitize_date_for_sql( $_GET['date_second'] ); + $date_first = yourls_sanitize_date( $_GET['date_first'] ); + $date_second = yourls_sanitize_date( $_GET['date_second'] ); $where .= " AND `timestamp` BETWEEN '$date_first_sql' AND '$date_second_sql'"; - $date_first = $_GET['date_first']; - $date_second = $_GET['date_second']; } break; } } // Sorting -if( !empty($_GET['s_by']) || !empty($_GET['s_order']) ) { - switch($_GET['s_by']) { +if( !empty( $_GET['sort_by'] ) || !empty( $_GET['sort_order'] ) ) { + switch( $_GET['sort_by'] ) { case 'keyword': - $sort_by_text = 'Short URL'; - $sort_by_sql = 'keyword'; + $sort_by_text = yourls__( 'Short URL' ); + $sort_by = 'keyword'; break; case 'url': - $sort_by_text = 'URL'; - $sort_by_sql = 'url'; + $sort_by_text = yourls__( 'URL' ); + $sort_by = 'url'; break; case 'timestamp': - $sort_by_text = 'Date'; - $sort_by_sql = 'timestamp'; + $sort_by_text = yourls__( 'Date' ); + $sort_by = 'timestamp'; break; case 'ip': - $sort_by_text = 'IP Address'; - $sort_by_sql = 'ip'; + $sort_by_text = yourls__( 'IP Address' ); + $sort_by = 'ip'; break; case 'clicks': - $sort_by_text = 'Clicks'; - $sort_by_sql = 'clicks'; + $sort_by_text = yourls__( 'Clicks' ); + $sort_by = 'clicks'; break; } - switch($_GET['s_order']) { + switch( $_GET['sort_order'] ) { case 'asc': - $sort_order_text = 'Ascending Order'; - $sort_order_sql = 'asc'; + $sort_order_text = yourls__( 'Ascending Order' ); + $sort_order = 'asc'; break; case 'desc': - $sort_order_text = 'Descending Order'; - $sort_order_sql = 'desc'; + $sort_order_text = yourls__( 'Descending Order' ); + $sort_order = 'desc'; break; } } @@ -128,7 +128,7 @@ if ( $where ) { list( $total_items, $total_items_clicks ) = array_values( yourls_get_db_stats( $where ) ); } else { - $total_items = $total_urls; + $total_items = $total_urls; $total_items_clicks = false; } @@ -137,10 +137,10 @@ $is_bookmark = true; yourls_do_action( 'bookmarklet' ); - $url = yourls_sanitize_url( $_GET['u'] ); + $url = yourls_sanitize_url( $_GET['u'] ); $keyword = ( isset( $_GET['k'] ) ? yourls_sanitize_keyword( $_GET['k'] ) : '' ); - $title = ( isset( $_GET['t'] ) ? yourls_sanitize_title( $_GET['t'] ) : '' ); - $return = yourls_add_new_link( $url, $keyword, $title ); + $title = ( isset( $_GET['t'] ) ? yourls_sanitize_title( $_GET['t'] ) : '' ); + $return = yourls_add_new_link( $url, $keyword, $title ); // If fails because keyword already exist, retry with no keyword if ( isset( $return['status'] ) && $return['status'] == 'fail' && isset( $return['code'] ) && $return['code'] == 'error:keyword' ) { @@ -151,21 +151,20 @@ // Stop here if bookmarklet with a JSON callback function if( isset( $_GET['jsonp'] ) && $_GET['jsonp'] == 'yourls' ) { - $short = $return['shorturl'] ? $return['shorturl'] : ''; + $short = $return['shorturl'] ? $return['shorturl'] : ''; $message = $return['message']; - header('Content-type: application/json'); + header( 'Content-type: application/json' ); echo yourls_apply_filter( 'bookmarklet_jsonp', "yourls_callback({'short_url':'$short','message':'$message'});" ); die(); } - $s_url = stripslashes( $url ); - $where = " AND `url` LIKE '$s_url' "; + $where = sprintf( " AND `url` LIKE '%s' ", yourls_escape( $url ) ); - $page = $total_pages = $perpage = 1; + $page = $total_pages = $perpage = 1; $offset = 0; - $text = ( isset( $_GET['s'] ) ? stripslashes( $_GET['s'] ) : '' ); + $text = ( isset( $_GET['s'] ) ? stripslashes( $_GET['s'] ) : '' ); // This is not a bookmarklet @@ -173,30 +172,35 @@ $is_bookmark = false; // Checking $page, $offset, $perpage - if(empty($page) || $page == 0) { $page = 1; } - if(empty($offset)) { $offset = 0; } - if(empty($perpage) || $perpage == 0) { $perpage = 50; } + if( empty($page) || $page == 0 ) { + $page = 1; + } + if( empty($offset) ) { + $offset = 0; + } + if( empty($perpage) || $perpage == 0) { + $perpage = 50; + } // Determine $offset - $offset = ($page-1) * $perpage; + $offset = ( $page-1 ) * $perpage; // Determine Max Number Of Items To Display On Page - if(($offset + $perpage) > $total_items) { + if( ( $offset + $perpage ) > $total_items ) { $max_on_page = $total_items; } else { - $max_on_page = ($offset + $perpage); + $max_on_page = ( $offset + $perpage ); } // Determine Number Of Items To Display On Page - if (($offset + 1) > ($total_items)) { + if ( ( $offset + 1 ) > $total_items ) { $display_on_page = $total_items; } else { - $display_on_page = ($offset + 1); + $display_on_page = ( $offset + 1 ); } // Determing Total Amount Of Pages - $total_pages = ceil($total_items / $perpage); - + $total_pages = ceil( $total_items / $perpage ); } @@ -207,11 +211,16 @@ yourls_html_menu() ; yourls_do_action( 'admin_page_before_content' ); + if ( !$is_bookmark ) { ?> -

-

%d to %d of %d URLs'), $display_on_page, $max_on_page, $total_items ); if( $total_items_clicks !== false ) echo ", " . yourls__('counting') . " $total_items_clicks " . yourls_plural(yourls__('click'), $total_items_clicks); ?>.

+

+

%d to %d of %d URLs' ), $display_on_page, $max_on_page, $total_items ); + if( $total_items_clicks !== false ) + echo ", " . yourls__('counting') . " $total_items_clicks " . yourls_plural(yourls__('click'), $total_items_clicks); + ?>.

-

%d links, %d clicks, and counting!'), number_format($total_urls), number_format($total_clicks) ); ?>

+

%d links, %d clicks, and counting!' ), number_format( $total_urls ), number_format( $total_clicks ) ); ?>

@@ -234,19 +243,19 @@ if ( !$is_bookmark ) { $params = array( - 'search_text' => $search_text, - 'search_in_sql' => $search_in_sql, - 's_by' => $sort_by_sql, - 'sort_order_sql' => $sort_order_sql, - 'page' => $page, - 'perpage' => $perpage, - 'link_filter' => $link_filter, - 'link_limit' => $link_limit, - 'total_pages' => $total_pages, - 'search_url' => $search_url, - 'date_filter' => $date_filter, - 'date_first' => $date_first, - 'date_second' => $date_second, + 'search' => $search, + 'search_text' => $search_text, + 'search_in' => $search_in, + 'sort_by' => $sort_by, + 'sort_order' => $sort_order, + 'page' => $page, + 'perpage' => $perpage, + 'click_filter' => $click_filter, + 'click_limit' => $click_limit, + 'total_pages' => $total_pages, + 'date_filter' => $date_filter, + 'date_first' => $date_first, + 'date_second' => $date_second, ); yourls_html_tfooter( $params ); } @@ -255,7 +264,7 @@ // Main Query $where = yourls_apply_filter( 'admin_list_where', $where ); -$url_results = $ydb->get_results("SELECT * FROM `$table_url` WHERE 1=1 $where ORDER BY `$sort_by_sql` $sort_order_sql LIMIT $offset, $perpage;"); +$url_results = $ydb->get_results( "SELECT * FROM `$table_url` WHERE 1=1 $where ORDER BY `$sort_by` $sort_order LIMIT $offset, $perpage;" ); $found_rows = false; if( $url_results ) { $found_rows = true; diff --git a/includes/functions-html.php b/includes/functions-html.php index b000a76..9132fa5 100644 --- a/includes/functions-html.php +++ b/includes/functions-html.php @@ -1,6 +1,9 @@ header and logo +/** + * Display

header and logo + * + */ function yourls_html_logo() { yourls_do_action( 'pre_html_logo' ); ?> @@ -12,7 +15,12 @@ function yourls_html_logo() { yourls_do_action( 'html_logo' ); } -// Display HTML head and tag +/** + * Display HTML head and tag + * + * @param string $context Context of the page (stats, index, infos, ...) + * @param string $title HTML title of the page + */ function yourls_html_head( $context = 'index', $title = '' ) { yourls_do_action( 'pre_html_head', $context, $title ); @@ -122,7 +130,10 @@ function yourls_html_head( $context = 'index', $title = '' ) { @@ -149,7 +165,7 @@ function yourls_html_addnew( $url = '', $keyword = '' ) {
: : : -
+ @@ -158,10 +174,16 @@ function yourls_html_addnew( $url = '', $keyword = '' ) { @@ -169,43 +191,45 @@ function yourls_html_tfooter( $params = array() ) {
- - - - + + - –   - + + + + + - + + - –   -  
+ – +
- - + + - -
+ +
- + > and @@ -220,6 +244,17 @@ function yourls_html_tfooter( $params = array() ) {
+ + +

'; - if ($share_title == '') - $share_title = '

' . yourls__('Quick Share') . '

'; + if ( $shortlink_title == '' ) + $shortlink_title = '

' . yourls__( 'Your short link' ) . '

'; + if ( $share_title == '' ) + $share_title = '

' . yourls__( 'Quick Share' ) . '

'; // Allow plugins to short-circuit the whole function $pre = yourls_apply_filter( 'shunt_share_box', false ); if ( false !== $pre ) return $pre; - $text = ( $text ? '"'.$text.'" ' : '' ); - $title = ( $title ? "$title " : '' ); - $share = yourls_esc_textarea( $title.$text.$shorturl ); - $count = 140 - strlen( $share ); + $text = ( $text ? '"'.$text.'" ' : '' ); + $title = ( $title ? "$title " : '' ); + $share = yourls_esc_textarea( $title.$text.$shorturl ); + $count = 140 - strlen( $share ); $hidden = ( $hidden ? 'style="display:none;"' : '' ); // Allow plugins to filter all data @@ -282,7 +320,7 @@ function yourls_share_box( $longurl, $shorturl, $title = '', $text='', $shortlin extract( $data ); $_share = rawurlencode( $share ); - $_url = rawurlencode( $shorturl ); + $_url = rawurlencode( $shorturl ); ?>
> @@ -292,9 +330,9 @@ function yourls_share_box( $longurl, $shorturl, $title = '', $text='', $shortlin -

-
+

-
+

- +

@@ -570,34 +639,37 @@ function yourls_login_screen( $error_msg = '' ) { die(); } -// Display the admin menu +/** + * Display the admin menu + * + */ function yourls_html_menu() { // Build menu links if( defined( 'YOURLS_USER' ) ) { - $logout_link = yourls_apply_filter( 'logout_link', yourls__('Hello') . ' ' . YOURLS_USER . ' (' . yourls__('Logout') . ')' ); + $logout_link = yourls_apply_filter( 'logout_link', sprintf( yourls__('Hello %s'), YOURLS_USER ) . ' (' . yourls__( 'Logout' ) . ')' ); } else { $logout_link = yourls_apply_filter( 'logout_link', '' ); } - $help_link = yourls_apply_filter( 'help_link', '' . yourls__('Help') . '' ); + $help_link = yourls_apply_filter( 'help_link', '' . yourls__( 'Help' ) . '' ); $admin_links = array(); $admin_sublinks = array(); $admin_links['admin'] = array( 'url' => yourls_admin_url( 'index.php' ), - 'title' => yourls__('Go to the admin interface'), - 'anchor' => yourls__('Admin interface') + 'title' => yourls__( 'Go to the admin interface' ), + 'anchor' => yourls__( 'Admin interface' ) ); if( yourls_is_admin() ) { $admin_links['tools'] = array( 'url' => yourls_admin_url( 'tools.php' ), - 'anchor' => yourls__('Tools') + 'anchor' => yourls__( 'Tools' ) ); $admin_links['plugins'] = array( 'url' => yourls_admin_url( 'plugins.php' ), - 'anchor' => yourls__('Manage Plugins') + 'anchor' => yourls__( 'Manage Plugins' ) ); $admin_sublinks['plugins'] = yourls_list_plugin_admin_pages(); } @@ -644,13 +716,19 @@ function yourls_html_menu() { */ } -// Wrapper to admin notices +/** + * Wrapper function to display admin notices + * + */ function yourls_add_notice( $message, $style = 'notice' ) { $message = yourls_notice_box( $message, $style ); yourls_add_action( 'admin_notices', create_function( '', "echo '$message';" ) ); } -// Return a formatted notice +/** + * Return a formatted notice + * + */ function yourls_notice_box( $message, $style = 'notice' ) { return << @@ -659,7 +737,10 @@ function yourls_notice_box( $message, $style = 'notice' ) { HTML; } -// Display a page +/** + * Display a page + * + */ function yourls_page( $page ) { $include = YOURLS_ABSPATH . "/pages/$page.php"; if( !file_exists($include) ) { diff --git a/includes/functions.php b/includes/functions.php index 5781922..fe38206 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1759,4 +1759,17 @@ function yourls_deprecated_function( $function, $version, $replacement = null ) else trigger_error( sprintf( yourls__('%1$s is deprecated since version %2$s with no alternative available.'), $function, $version ) ); } -} \ No newline at end of file +} + +/** + * Return the value if not an empty string + * + * Used with array_filter(), to remove empty keys but not keys with value 0 or false + * + * @since 1.6 + * @param mixed $val Value to test against '' + * @return bool True if not an empty string + */ +function yourls_return_if_not_empty_string( $val ) { + return( $val !== '' ); +} -- 2.45.0