В одном плагине есть вкладка "Логи" при переходе в которую я получаю ошибку нехватки памяти, хотя там всего несколько строк и памяти выделено по максимуму. Как можно оптимизировать код, чтобы не потребляло много памяти?
Ошибка по строке:
$page_counter = 1;
while( $total_pages > 0 ){
if( $page_counter === $page ){
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page_counter, ) ) ) . '" class="wpwh-btn wpwh-btn--primary wpwh-btn--sm mr-1">' . $page_counter . '</a>';
} else {
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page_counter, ) ) ) . '" class="btn--secondary">' . $page_counter . '</a>';
}
$page_counter++;
}
Полный код метода:
public function pagination( $args = array() ) {
$per_page = isset( $args['per_page'] ) ? intval( $args['per_page'] ) : 10;
$page = isset( $args['log_page'] ) ? intval( $args['log_page'] ) : 1;
$page_counter = 1;
$log_count = $this->get_log_count();
$total_pages = ceil( $log_count / $per_page );
$current_url = LOGPLUG()->helpers->get_current_url(false);
if( $page > $total_pages ){
$page = $total_pages;
}
if( $page < 1 ){
$page = 1;
}
$pagination_links_out = array();
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => 1, ) ) ) . '" class="btn--sm mr-1">' . LOGPLUG()->helpers->translate( '<<', 'logs' ) . '</a>';
if( $page <= 1 ){
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => 1, ) ) ) . '" class="btn--sm mr-1">' . LOGPLUG()->helpers->translate( '<', 'logs' ) . '</a>';
} else {
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => ($page-1), ) ) ) . '" class="btn--sm mr-1">' . LOGPLUG()->helpers->translate( '<', 'logs' ) . '</a>';
}
if( $total_pages > 3 ){
if( $page === 1 ){
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => 1, ) ) ) . '" class="primary btn--sm mr-1">' . 1 . '</a>';
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => 2, ) ) ) . '" class="secondary btn--sm mr-1">' . 2 . '</a>';
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => 3, ) ) ) . '" class="secondary btn--sm mr-1">' . 3 . '</a>';
} elseif( $page >= $total_pages ){
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page-2, ) ) ) . '" class="secondary btn--sm mr-1">' . ($page-2) . '</a>';
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page-1, ) ) ) . '" class="secondary btn--sm mr-1">' . ($page-1) . '</a>';
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page, ) ) ) . '" class="primary btn--sm mr-1">' . $page . '</a>';
} else {
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page-1, ) ) ) . '" class="secondary btn--sm mr-1">' . ($page-1) . '</a>';
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page, ) ) ) . '" class="primary btn--sm mr-1">' . $page . '</a>';
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page+1, ) ) ) . '" class="secondary btn--sm mr-1">' . ($page+1) . '</a>';
}
} else {
$page_counter = 1;
while( $total_pages > 0 ){
if( $page_counter === $page ){
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page_counter, ) ) ) . '" class="primary btn--sm mr-1">' . $page_counter . '</a>';
} else {
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $page_counter, ) ) ) . '" class="secondary btn--sm mr-1">' . $page_counter . '</a>';
}
$page_counter++;
}
}
if( $page >= $total_pages ){
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $total_pages, ) ) ) . '" class="btn--sm mr-1">' . LOGPLUG()->helpers->translate( '>', 'logs' ) . '</a>';
} else {
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => ($page+1), ) ) ) . '" class="btn--sm mr-1">' . LOGPLUG()->helpers->translate( '>', 'logs' ) . '</a>';
}
$pagination_links_out[] = '<a href="' . LOGPLUG()->helpers->built_url( $current_url, array_merge( $_GET, array( 'item_count' => $per_page, 'log_page' => $total_pages, ) ) ) . '" class="btn--sm mr-1">' . LOGPLUG()->helpers->translate( '>>', 'logs' ) . '</a>';
return implode( '', $pagination_links_out );
}