| [ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 function gp_tmpl_load( $template, $args = array(), $template_path = null ) { 3 $args = gp_tmpl_filter_args( $args ); 4 do_action_ref_array( 'pre_tmpl_load', array( $template, &$args ) ); 5 require_once GP_TMPL_PATH . 'helper-functions.php'; 6 $locations = array( GP_TMPL_PATH ); 7 if ( !is_null( $template_path ) ) { 8 array_unshift( $locations, untrailingslashit( $template_path ) . '/' ); 9 } 10 if ( isset( $args['http_status'] ) ) 11 status_header( $args['http_status'] ); 12 foreach( $locations as $location ) { 13 $file = $location . "$template.php"; 14 if ( is_readable( $file ) ) { 15 extract( $args, EXTR_SKIP ); 16 include $file; 17 break; 18 } 19 } 20 do_action_ref_array( 'post_tmpl_load', array( $template, &$args ) ); 21 } 22 23 function gp_tmpl_get_output() { 24 $args = func_get_args(); 25 ob_start(); 26 call_user_func_array( 'gp_tmpl_load', $args ); 27 $contents = ob_get_contents(); 28 ob_end_clean(); 29 return $contents; 30 } 31 32 function gp_tmpl_header( $args = array( ) ) { 33 gp_tmpl_load( 'header', $args ); 34 } 35 36 function gp_tmpl_footer( $args = array( ) ) { 37 gp_tmpl_load( 'footer', $args ); 38 } 39 40 function gp_head() { 41 do_action( 'gp_head' ); 42 } 43 44 function gp_footer() { 45 do_action( 'gp_footer' ); 46 } 47 48 function gp_content() { 49 do_action( 'gp_content' ); 50 } 51 52 function gp_tmpl_filter_args( $args ) { 53 $clean_args = array(); 54 foreach( $args as $k => $v ) 55 if ( $k[0] != '_' && $k != 'GLOBALS' && !gp_startswith( $k, 'HTTP' ) && !gp_startswith( $k, 'PHP' ) ) 56 $clean_args[$k] = $v; 57 return $clean_args; 58 } 59 60 function gp_tmpl_404( $args = array()) { 61 gp_tmpl_load( '404', $args + array('title' => __('Not Found'), 'http_status' => 404 ) ); 62 exit(); 63 } 64 65 function gp_title( $title = null ) { 66 if ( !is_null( $title ) ) 67 add_filter( 'gp_title', lambda( '$x', '$title', compact( 'title' ) ), 5 ); 68 else 69 return apply_filters( 'gp_title', '' ); 70 } 71 72 function gp_breadcrumb( $breadcrumb = null, $args = array() ) { 73 $defaults = array( 74 /* translators: separates links in the navigation breadcrumb */ 75 'separator' => '<span class="separator">'._x('→', 'breadcrumb').'</span>', 76 'breadcrumb-template' => '<span class="breadcrumb">{separator}{breadcrumb}</span>', 77 ); 78 $args = array_merge( $defaults, $args ); 79 if ( !is_null( $breadcrumb ) ) { 80 $breadcrumb = gp_array_flatten( $breadcrumb ); 81 $breadcrumb_string = implode( $args['separator'], array_filter( $breadcrumb ) ); 82 $whole_breadcrumb = str_replace( '{separator}', $args['separator'], $args['breadcrumb-template'] ); 83 $whole_breadcrumb = str_replace( '{breadcrumb}', $breadcrumb_string, $whole_breadcrumb ); 84 add_filter( 'gp_breadcrumb', lambda( '$x', '$whole_breadcrumb', compact( 'whole_breadcrumb' ) ), 5 ); 85 } else { 86 return apply_filters( 'gp_breadcrumb', '' ); 87 } 88 } 89 90 function gp_project_links_from_root( $leaf_project ) { 91 $links = array(); 92 $path_from_root = array_reverse( $leaf_project->path_to_root() ); 93 $links[] = empty( $path_from_root)? 'Projects' : gp_link_get( gp_url( '/projects' ), 'Projects' ); 94 foreach( $path_from_root as $project ) { 95 $links[] = gp_link_project_get( $project, esc_html( $project->name ) ); 96 } 97 return $links; 98 } 99 100 function gp_breadcrumb_project( $project ) { 101 return gp_breadcrumb( gp_project_links_from_root( $project ) ); 102 } 103 104 function gp_js_focus_on( $html_id ) { 105 return '<script type="text/javascript">document.getElementById("'.$html_id.'").focus();</script>'; 106 } 107 108 function gp_select( $name_and_id, $options, $selected_key, $attrs = array() ) { 109 $attributes = gp_html_attributes( $attrs ); 110 $attributes = $attributes? " $attributes" : ''; 111 $res = "<select name='" . esc_attr( $name_and_id ) . "' id='" . esc_attr( $name_and_id ) . "' $attributes>\n"; 112 foreach( $options as $value => $label ) { 113 $selected = $value == $selected_key? " selected='selected'" : ''; 114 $res .= "\t<option value='".esc_attr( $value )."' $selected>" . esc_html( $label ) . "</option>\n"; 115 } 116 $res .= "</select>\n"; 117 return $res; 118 } 119 120 function gp_radio_buttons( $name, $radio_buttons, $checked_key ) { 121 $res = ''; 122 foreach( $radio_buttons as $value => $label ) { 123 $checked = $value == $checked_key? " checked='checked'" : ''; 124 // TODO: something more flexible than <br /> 125 $res .= "\t<input type='radio' name='$name' value='".esc_attr( $value )."' $checked id='{$name}[{$value}]'/> "; 126 $res .= "<label for='{$name}[{$value}]'>".esc_html( $label )."</label><br />\n"; 127 } 128 return $res; 129 } 130 131 function gp_pagination( $page, $per_page, $objects ) { 132 $surrounding = 2; 133 $prev = $first = $prev_dots = $prev_pages = $current = $next_pages = $next_dots = $last = $next = ''; 134 $page = intval( $page )? intval( $page ) : 1; 135 $pages = ceil( $objects / $per_page ); 136 if ( $page > $pages ) return ''; 137 if ( $page > 1 ) 138 $prev = gp_link_get( add_query_arg( array( 'page' => $page - 1 ) ), '←', array('class' => 'previous') ); 139 else 140 $prev = '<span class="previous disabled">←</span>'; 141 if ( $page < $pages ) 142 $next = gp_link_get( add_query_arg( array( 'page' => $page + 1)), '→', array('class' => 'next') ); 143 else 144 $next = '<span class="next disabled">→</span>'; 145 $current = '<span class="current">'.$page.'</span>'; 146 if ( $page > 1 ) { 147 $prev_pages = array(); 148 foreach( range( max( 1, $page - $surrounding ), $page - 1 ) as $prev_page ) { 149 $prev_pages[] = gp_link_get( add_query_arg( array( 'page' => $prev_page ) ), $prev_page ); 150 } 151 $prev_pages = implode( ' ', $prev_pages ); 152 if ( $page - $surrounding > 1 ) $prev_dots = '<span class="dots">…</span>'; 153 } 154 if ( $page < $pages ) { 155 $next_pages = array(); 156 foreach( range( $page + 1, min( $pages, $page + $surrounding ) ) as $next_page ) { 157 $next_pages[] = gp_link_get( add_query_arg( array( 'page' => $next_page ) ), $next_page ); 158 } 159 $next_pages = implode( ' ', $next_pages ); 160 if ( $page + $surrounding < $pages ) $next_dots = '<span class="dots">…</span>'; 161 } 162 if ( $prev_dots ) $first = gp_link_get( add_query_arg( array( 'page' => 1 ) ), 1 ); 163 if ( $next_dots ) $last = gp_link_get( add_query_arg( array( 'page' => $pages ) ), $pages ); 164 $html = <<<HTML 165 <div class="paging"> 166 $prev 167 $first 168 $prev_dots 169 $prev_pages 170 $current 171 $next_pages 172 $next_dots 173 $last 174 $next 175 </div> 176 HTML; 177 return apply_filters( 'gp_pagination', $html, $page, $per_page, $objects ); 178 } 179 180 function gp_html_attributes( $attrs ) { 181 $attrs = wp_parse_args( $attrs ); 182 $strings = array(); 183 foreach( $attrs as $key => $value ) { 184 $strings[] = $key.'="'.esc_attr( $value ).'"'; 185 } 186 return implode( ' ', $strings ); 187 } 188 189 function gp_attrs_add_class( $attrs, $class_name ) { 190 $attrs['class'] = isset( $attrs['class'] )? $attrs['class'] . ' ' . $class_name : $class_name; 191 return $attrs; 192 } 193 194 function gp_locales_dropdown( $name_and_id, $selected_slug = null, $attrs = array() ) { 195 $locales = GP_Locales::locales(); 196 $values = array_map( create_function( '$l', 'return $l->slug;'), $locales ); 197 $labels = array_map( create_function( '$l', 'return $l->slug." — ". $l->english_name;'), $locales ); 198 sort( $values ); 199 sort( $labels ); 200 return gp_select( $name_and_id, array_merge( array( '' => __('— Locale —') ), array_combine( $values, $labels ) ), $selected_slug, $attrs ); 201 } 202 203 function gp_projects_dropdown( $name_and_id, $selected_project_id = null, $attrs = array() ) { 204 $projects = GP::$project->all(); 205 // TODO: mark which nodes are editable by the current user 206 $tree = array(); 207 $top = array(); 208 foreach( $projects as $p ) { 209 $tree[$p->id]['self'] = $p; 210 if ( $p->parent_project_id ) { 211 $tree[$p->parent_project_id]['children'][] = $p->id; 212 } else { 213 $top[] = $p->id; 214 } 215 } 216 $options = array( '' => __('— No parent —') ); 217 $stack = array(); 218 foreach( $top as $top_id ) { 219 $stack = array( $top_id ); 220 while ( !empty( $stack ) ) { 221 $id = array_pop( $stack ); 222 $tree[$id]['level'] = gp_array_get( $tree[$id], 'level', 0 ); 223 $options[$id] = str_repeat( '-', $tree[$id]['level'] ) . $tree[$id]['self']->name; 224 foreach( gp_array_get( $tree[$id], 'children', array() ) as $child_id ) { 225 $stack[] = $child_id; 226 $tree[$child_id]['level'] = $tree[$id]['level'] + 1; 227 } 228 } 229 } 230 return gp_select( $name_and_id, $options, $selected_project_id, $attrs ); 231 } 232 233 function gp_array_of_things_to_json( $array ) { 234 return json_encode( array_map( lambda( '$thing', '$thing->fields();' ), $array ) ); 235 } 236 237 function gp_array_of_array_of_things_to_json( $array ) { 238 $map_to_fields = create_function( '$array', 'return array_map( lambda( \'$thing\', \'$thing->fields();\' ), $array );' ); 239 return json_encode( array_map( $map_to_fields, $array ) ); 240 } 241 242 function gp_preferred_sans_serif_style_tag( $locale ) { 243 if ( $locale->preferred_sans_serif_font_family ) { 244 echo <<<HTML 245 <style type="text/css"> 246 .foreign-text { 247 font-family: "$locale->preferred_sans_serif_font_family", inherit; 248 } 249 </style> 250 251 HTML; 252 } 253 } 254 255 function gp_html_excerpt( $str, $count, $ellipsis = '…') { 256 $excerpt = trim( wp_html_excerpt( $str, $count ) ); 257 if ( $str != $excerpt ) { 258 $excerpt .= $ellipsis; 259 } 260 return $excerpt; 261 } 262 263 function gp_checked( $checked ) { 264 if ( $checked ) { 265 echo 'checked="checked"'; 266 } 267 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu May 24 03:59:35 2012 | Hosted by follow the white rabbit. |