$label ) { if ( 'date' === $key ) { $out['kdocs_kb'] = __( 'Knowledge Base', 'karma-docs' ); $out['kdocs_views'] = __( 'Views', 'karma-docs' ); $out['kdocs_helpful'] = __( 'Helpful', 'karma-docs' ); } $out[ $key ] = $label; } return $out; } /** * Column content. * * @param string $column Column. * @param int $post_id Post. */ public function column( $column, $post_id ) { switch ( $column ) { case 'kdocs_kb': $kb = Taxonomies::article_kb( $post_id ); if ( $kb ) { printf( '%s', esc_url( add_query_arg( 'kdocs_kb', $kb->slug, admin_url( 'edit.php?post_type=' . PostTypes::ARTICLE ) ) ), esc_html( $kb->name ) ); } else { echo '—'; } break; case 'kdocs_views': echo esc_html( number_format_i18n( (int) get_post_meta( $post_id, '_kdocs_views', true ) ) ); break; case 'kdocs_helpful': $yes = (int) get_post_meta( $post_id, '_kdocs_helpful_yes', true ); $no = (int) get_post_meta( $post_id, '_kdocs_helpful_no', true ); $total = $yes + $no; if ( ! $total ) { echo '—'; break; } $score = round( $yes / $total * 100 ); $color = $score >= 70 ? '#16a34a' : ( $score >= 40 ? '#d97706' : '#dc2626' ); printf( '%3$d%% (%4$s)', esc_attr( $color ), /* translators: 1: yes votes, 2: no votes */ esc_attr( sprintf( __( '%1$d helpful, %2$d not helpful', 'karma-docs' ), $yes, $no ) ), (int) $score, esc_html( number_format_i18n( $total ) ) ); break; } } /** * Sortable columns. * * @param array $columns Columns. * @return array */ public function sortable( $columns ) { $columns['kdocs_views'] = 'kdocs_views'; return $columns; } /** * KB filter dropdown. * * @param string $post_type Post type. */ public function filter( $post_type ) { if ( PostTypes::ARTICLE !== $post_type ) { return; } $current = isset( $_GET['kdocs_kb'] ) ? sanitize_title( wp_unslash( $_GET['kdocs_kb'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification echo ''; } /** * Applies sorting by views. * * @param \WP_Query $query Query. */ public function query( $query ) { if ( ! is_admin() || ! $query->is_main_query() || PostTypes::ARTICLE !== $query->get( 'post_type' ) ) { return; } if ( 'kdocs_views' === $query->get( 'orderby' ) ) { $query->set( 'meta_key', '_kdocs_views' ); $query->set( 'orderby', 'meta_value_num' ); } } /** * "Open in builder" row action. * * @param array $actions Actions. * @param \WP_Post $post Post. * @return array */ public function row_actions( $actions, $post ) { if ( PostTypes::ARTICLE !== $post->post_type ) { return $actions; } $kb = Taxonomies::article_kb( $post ); if ( $kb ) { $actions['kdocs_builder'] = sprintf( '%s', esc_url( admin_url( 'admin.php?page=karma-docs-builder&kb=' . $kb->term_id . '&node=a' . $post->ID ) ), esc_html__( 'Open in builder', 'karma-docs' ) ); } return $actions; } /** * Column widths. */ public function styles() { $screen = get_current_screen(); if ( ! $screen || PostTypes::ARTICLE !== $screen->post_type ) { return; } echo ''; } }