$v ) if ( $k[0] != '_' && $k != 'GLOBALS' && !gp_startswith( $k, 'HTTP' ) && !gp_startswith( $k, 'PHP' ) ) $clean_args[$k] = $v; return $clean_args; } function gp_tmpl_404( $args = array()) { gp_tmpl_load( '404', $args + array('title' => __('Not Found'), 'http_status' => 404 ) ); exit(); } function gp_title( $title = null ) { if ( !is_null( $title ) ) add_filter( 'gp_title', lambda( '$x', '$title', compact( 'title' ) ), 5 ); else return apply_filters( 'gp_title', '' ); } function gp_breadcrumb( $breadcrumb = null, $args = array() ) { $defaults = array( /* translators: separates links in the navigation breadcrumb */ 'separator' => ''._x('→', 'breadcrumb').'', 'breadcrumb-template' => '{separator}{breadcrumb}', ); $args = array_merge( $defaults, $args ); if ( !is_null( $breadcrumb ) ) { $breadcrumb = gp_array_flatten( $breadcrumb ); $breadcrumb_string = implode( $args['separator'], array_filter( $breadcrumb ) ); $whole_breadcrumb = str_replace( '{separator}', $args['separator'], $args['breadcrumb-template'] ); $whole_breadcrumb = str_replace( '{breadcrumb}', $breadcrumb_string, $whole_breadcrumb ); add_filter( 'gp_breadcrumb', lambda( '$x', '$whole_breadcrumb', compact( 'whole_breadcrumb' ) ), 5 ); } else { return apply_filters( 'gp_breadcrumb', '' ); } } function gp_project_links_from_root( $leaf_project ) { $links = array(); $path_from_root = array_reverse( $leaf_project->path_to_root() ); $links[] = empty( $path_from_root)? 'Projects' : gp_link_get( gp_url( '/projects' ), 'Projects' ); foreach( $path_from_root as $project ) { $links[] = gp_link_project_get( $project, esc_html( $project->name ) ); } return $links; } function gp_breadcrumb_project( $project ) { return gp_breadcrumb( gp_project_links_from_root( $project ) ); } function gp_js_focus_on( $html_id ) { return ''; } function gp_select( $name_and_id, $options, $selected_key, $attrs = array() ) { $attributes = gp_html_attributes( $attrs ); $attributes = $attributes? " $attributes" : ''; $res = "\n"; return $res; } function gp_radio_buttons( $name, $radio_buttons, $checked_key ) { $res = ''; foreach( $radio_buttons as $value => $label ) { $checked = $value == $checked_key? " checked='checked'" : ''; // TODO: something more flexible than
$res .= "\t "; $res .= "
\n"; } return $res; } function gp_pagination( $page, $per_page, $objects ) { $surrounding = 2; $prev = $first = $prev_dots = $prev_pages = $current = $next_pages = $next_dots = $last = $next = ''; $page = intval( $page )? intval( $page ) : 1; $pages = ceil( $objects / $per_page ); if ( $page > $pages ) return ''; if ( $page > 1 ) $prev = gp_link_get( add_query_arg( array( 'page' => $page - 1 ) ), '←', array('class' => 'previous') ); else $prev = ''; if ( $page < $pages ) $next = gp_link_get( add_query_arg( array( 'page' => $page + 1)), '→', array('class' => 'next') ); else $next = ''; $current = ''.$page.''; if ( $page > 1 ) { $prev_pages = array(); foreach( range( max( 1, $page - $surrounding ), $page - 1 ) as $prev_page ) { $prev_pages[] = gp_link_get( add_query_arg( array( 'page' => $prev_page ) ), $prev_page ); } $prev_pages = implode( ' ', $prev_pages ); if ( $page - $surrounding > 1 ) $prev_dots = ''; } if ( $page < $pages ) { $next_pages = array(); foreach( range( $page + 1, min( $pages, $page + $surrounding ) ) as $next_page ) { $next_pages[] = gp_link_get( add_query_arg( array( 'page' => $next_page ) ), $next_page ); } $next_pages = implode( ' ', $next_pages ); if ( $page + $surrounding < $pages ) $next_dots = ''; } if ( $prev_dots ) $first = gp_link_get( add_query_arg( array( 'page' => 1 ) ), 1 ); if ( $next_dots ) $last = gp_link_get( add_query_arg( array( 'page' => $pages ) ), $pages ); $html = << $prev $first $prev_dots $prev_pages $current $next_pages $next_dots $last $next HTML; return apply_filters( 'gp_pagination', $html, $page, $per_page, $objects ); } function gp_html_attributes( $attrs ) { $attrs = wp_parse_args( $attrs ); $strings = array(); foreach( $attrs as $key => $value ) { $strings[] = $key.'="'.esc_attr( $value ).'"'; } return implode( ' ', $strings ); } function gp_attrs_add_class( $attrs, $class_name ) { $attrs['class'] = isset( $attrs['class'] )? $attrs['class'] . ' ' . $class_name : $class_name; return $attrs; } function gp_locales_dropdown( $name_and_id, $selected_slug = null, $attrs = array() ) { $locales = GP_Locales::locales(); $values = array_map( create_function( '$l', 'return $l->slug;'), $locales ); $labels = array_map( create_function( '$l', 'return $l->slug." — ". $l->english_name;'), $locales ); sort( $values ); sort( $labels ); return gp_select( $name_and_id, array_merge( array( '' => __('— Locale —') ), array_combine( $values, $labels ) ), $selected_slug, $attrs ); } function gp_projects_dropdown( $name_and_id, $selected_project_id = null, $attrs = array() ) { $projects = GP::$project->all(); // TODO: mark which nodes are editable by the current user $tree = array(); $top = array(); foreach( $projects as $p ) { $tree[$p->id]['self'] = $p; if ( $p->parent_project_id ) { $tree[$p->parent_project_id]['children'][] = $p->id; } else { $top[] = $p->id; } } $options = array( '' => __('— No parent —') ); $stack = array(); foreach( $top as $top_id ) { $stack = array( $top_id ); while ( !empty( $stack ) ) { $id = array_pop( $stack ); $tree[$id]['level'] = gp_array_get( $tree[$id], 'level', 0 ); $options[$id] = str_repeat( '-', $tree[$id]['level'] ) . $tree[$id]['self']->name; foreach( gp_array_get( $tree[$id], 'children', array() ) as $child_id ) { $stack[] = $child_id; $tree[$child_id]['level'] = $tree[$id]['level'] + 1; } } } return gp_select( $name_and_id, $options, $selected_project_id, $attrs ); } function gp_array_of_things_to_json( $array ) { return json_encode( array_map( lambda( '$thing', '$thing->fields();' ), $array ) ); } function gp_array_of_array_of_things_to_json( $array ) { $map_to_fields = create_function( '$array', 'return array_map( lambda( \'$thing\', \'$thing->fields();\' ), $array );' ); return json_encode( array_map( $map_to_fields, $array ) ); } function gp_preferred_sans_serif_style_tag( $locale ) { if ( $locale->preferred_sans_serif_font_family ) { echo << .foreign-text { font-family: "$locale->preferred_sans_serif_font_family", inherit; } HTML; } } function gp_html_excerpt( $str, $count, $ellipsis = '…') { $excerpt = trim( wp_html_excerpt( $str, $count ) ); if ( $str != $excerpt ) { $excerpt .= $ellipsis; } return $excerpt; } function gp_checked( $checked ) { if ( $checked ) { echo 'checked="checked"'; } }