| [ 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 body_class() { 73 $classes = apply_filters( 'body_class', array( 'no-js' ) ); 74 75 // Separates classes with a single space, collates classes for body element 76 echo 'class="' . join( ' ', $classes ) . '"'; 77 } 78 79 80 function gp_breadcrumb( $breadcrumb = null, $args = array() ) { 81 $defaults = array( 82 /* translators: separates links in the navigation breadcrumb */ 83 'separator' => '<span class="separator">'._x('→', 'breadcrumb').'</span>', 84 'breadcrumb-template' => '<span class="breadcrumb">{separator}{breadcrumb}</span>', 85 ); 86 $args = array_merge( $defaults, $args ); 87 if ( !is_null( $breadcrumb ) ) { 88 $breadcrumb = gp_array_flatten( $breadcrumb ); 89 $breadcrumb_string = implode( $args['separator'], array_filter( $breadcrumb ) ); 90 $whole_breadcrumb = str_replace( '{separator}', $args['separator'], $args['breadcrumb-template'] ); 91 $whole_breadcrumb = str_replace( '{breadcrumb}', $breadcrumb_string, $whole_breadcrumb ); 92 add_filter( 'gp_breadcrumb', lambda( '$x', '$whole_breadcrumb', compact( 'whole_breadcrumb' ) ), 5 ); 93 } else { 94 return apply_filters( 'gp_breadcrumb', '' ); 95 } 96 } 97 98 function gp_project_links_from_root( $leaf_project ) { 99 $links = array(); 100 $path_from_root = array_reverse( $leaf_project->path_to_root() ); 101 $links[] = empty( $path_from_root)? __('Projects') : gp_link_get( gp_url( '/projects' ), __('Projects') ); 102 foreach( $path_from_root as $project ) { 103 $links[] = gp_link_project_get( $project, esc_html( $project->name ) ); 104 } 105 return $links; 106 } 107 108 function gp_breadcrumb_project( $project ) { 109 return gp_breadcrumb( gp_project_links_from_root( $project ) ); 110 } 111 112 function gp_js_focus_on( $html_id ) { 113 return '<script type="text/javascript">document.getElementById("'.$html_id.'").focus();</script>'; 114 } 115 116 function gp_select( $name_and_id, $options, $selected_key, $attrs = array() ) { 117 $attributes = gp_html_attributes( $attrs ); 118 $attributes = $attributes? " $attributes" : ''; 119 $res = "<select name='" . esc_attr( $name_and_id ) . "' id='" . esc_attr( $name_and_id ) . "' $attributes>\n"; 120 foreach( $options as $value => $label ) { 121 $selected = $value == $selected_key? " selected='selected'" : ''; 122 $res .= "\t<option value='".esc_attr( $value )."' $selected>" . esc_html( $label ) . "</option>\n"; 123 } 124 $res .= "</select>\n"; 125 return $res; 126 } 127 128 function gp_radio_buttons( $name, $radio_buttons, $checked_key ) { 129 $res = ''; 130 foreach( $radio_buttons as $value => $label ) { 131 $checked = $value == $checked_key? " checked='checked'" : ''; 132 // TODO: something more flexible than <br /> 133 $res .= "\t<input type='radio' name='$name' value='".esc_attr( $value )."' $checked id='{$name}[{$value}]'/> "; 134 $res .= "<label for='{$name}[{$value}]'>".esc_html( $label )."</label><br />\n"; 135 } 136 return $res; 137 } 138 139 function gp_pagination( $page, $per_page, $objects ) { 140 $surrounding = 2; 141 $prev = $first = $prev_dots = $prev_pages = $current = $next_pages = $next_dots = $last = $next = ''; 142 $page = intval( $page )? intval( $page ) : 1; 143 $pages = ceil( $objects / $per_page ); 144 if ( $page > $pages ) return ''; 145 if ( $page > 1 ) 146 $prev = gp_link_get( add_query_arg( array( 'page' => $page - 1 ) ), '←', array('class' => 'previous') ); 147 else 148 $prev = '<span class="previous disabled">←</span>'; 149 if ( $page < $pages ) 150 $next = gp_link_get( add_query_arg( array( 'page' => $page + 1)), '→', array('class' => 'next') ); 151 else 152 $next = '<span class="next disabled">→</span>'; 153 $current = '<span class="current">'.$page.'</span>'; 154 if ( $page > 1 ) { 155 $prev_pages = array(); 156 foreach( range( max( 1, $page - $surrounding ), $page - 1 ) as $prev_page ) { 157 $prev_pages[] = gp_link_get( add_query_arg( array( 'page' => $prev_page ) ), $prev_page ); 158 } 159 $prev_pages = implode( ' ', $prev_pages ); 160 if ( $page - $surrounding > 1 ) $prev_dots = '<span class="dots">…</span>'; 161 } 162 if ( $page < $pages ) { 163 $next_pages = array(); 164 foreach( range( $page + 1, min( $pages, $page + $surrounding ) ) as $next_page ) { 165 $next_pages[] = gp_link_get( add_query_arg( array( 'page' => $next_page ) ), $next_page ); 166 } 167 $next_pages = implode( ' ', $next_pages ); 168 if ( $page + $surrounding < $pages ) $next_dots = '<span class="dots">…</span>'; 169 } 170 if ( $prev_dots ) $first = gp_link_get( add_query_arg( array( 'page' => 1 ) ), 1 ); 171 if ( $next_dots ) $last = gp_link_get( add_query_arg( array( 'page' => $pages ) ), $pages ); 172 $html = <<<HTML 173 <div class="paging"> 174 $prev 175 $first 176 $prev_dots 177 $prev_pages 178 $current 179 $next_pages 180 $next_dots 181 $last 182 $next 183 </div> 184 HTML; 185 return apply_filters( 'gp_pagination', $html, $page, $per_page, $objects ); 186 } 187 188 function gp_html_attributes( $attrs ) { 189 $attrs = wp_parse_args( $attrs ); 190 $strings = array(); 191 foreach( $attrs as $key => $value ) { 192 $strings[] = $key.'="'.esc_attr( $value ).'"'; 193 } 194 return implode( ' ', $strings ); 195 } 196 197 function gp_attrs_add_class( $attrs, $class_name ) { 198 $attrs['class'] = isset( $attrs['class'] )? $attrs['class'] . ' ' . $class_name : $class_name; 199 return $attrs; 200 } 201 202 function gp_locales_dropdown( $name_and_id, $selected_slug = null, $attrs = array() ) { 203 $locales = GP_Locales::locales(); 204 $values = array_map( create_function( '$l', 'return $l->slug;'), $locales ); 205 $labels = array_map( create_function( '$l', 'return $l->slug." — ". $l->english_name;'), $locales ); 206 sort( $values ); 207 sort( $labels ); 208 return gp_select( $name_and_id, array_merge( array( '' => __('— Locale —') ), array_combine( $values, $labels ) ), $selected_slug, $attrs ); 209 } 210 211 function gp_projects_dropdown( $name_and_id, $selected_project_id = null, $attrs = array() ) { 212 $projects = GP::$project->all(); 213 // TODO: mark which nodes are editable by the current user 214 $tree = array(); 215 $top = array(); 216 foreach( $projects as $p ) { 217 $tree[$p->id]['self'] = $p; 218 if ( $p->parent_project_id ) { 219 $tree[$p->parent_project_id]['children'][] = $p->id; 220 } else { 221 $top[] = $p->id; 222 } 223 } 224 $options = array( '' => __('— No parent —') ); 225 $stack = array(); 226 foreach( $top as $top_id ) { 227 $stack = array( $top_id ); 228 while ( !empty( $stack ) ) { 229 $id = array_pop( $stack ); 230 $tree[$id]['level'] = gp_array_get( $tree[$id], 'level', 0 ); 231 $options[$id] = str_repeat( '-', $tree[$id]['level'] ) . $tree[$id]['self']->name; 232 foreach( gp_array_get( $tree[$id], 'children', array() ) as $child_id ) { 233 $stack[] = $child_id; 234 $tree[$child_id]['level'] = $tree[$id]['level'] + 1; 235 } 236 } 237 } 238 return gp_select( $name_and_id, $options, $selected_project_id, $attrs ); 239 } 240 241 function gp_array_of_things_to_json( $array ) { 242 return json_encode( array_map( lambda( '$thing', '$thing->fields();' ), $array ) ); 243 } 244 245 function gp_array_of_array_of_things_to_json( $array ) { 246 $map_to_fields = create_function( '$array', 'return array_map( lambda( \'$thing\', \'$thing->fields();\' ), $array );' ); 247 return json_encode( array_map( $map_to_fields, $array ) ); 248 } 249 250 function gp_preferred_sans_serif_style_tag( $locale ) { 251 if ( $locale->preferred_sans_serif_font_family ) { 252 echo <<<HTML 253 <style type="text/css"> 254 .foreign-text { 255 font-family: "$locale->preferred_sans_serif_font_family", inherit; 256 } 257 </style> 258 259 HTML; 260 } 261 } 262 263 function gp_html_excerpt( $str, $count, $ellipsis = '…') { 264 $excerpt = trim( wp_html_excerpt( $str, $count ) ); 265 if ( $str != $excerpt ) { 266 $excerpt .= $ellipsis; 267 } 268 return $excerpt; 269 } 270 271 function gp_checked( $checked ) { 272 if ( $checked ) { 273 echo 'checked="checked"'; 274 } 275 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri May 24 04:00:00 2013 | Hosted by follow the white rabbit. |