Функция обратного вызова - PullRequest
2 голосов
/ 11 мая 2011

У меня есть функция обратного вызова для другой функции, которая просматривает комментарии к статье в моем блоге. Теперь я попытался переключиться на «резьбовые / вложенные» комментарии и поэтому расширил функцию обратного вызова. Пока все работает, но я не могу избавиться от ощущения, что я не написал это в соответствии с лучшей практикой php (и производительностью).

Я использую css-фреймворк и должен выполнить некоторые математические расчеты для .span-xy классов, которые я назначаю отдельным комментариям. Я начинаю с входного значения из глобальной константы и вывода, например. span-12 за родительский комментарий. Затем я должен уменьшить / увеличить значение за +/- (int) 1 для каждого уровня вложенности. Поэтому я пришел к созданию массивов, их подсчету, повторению и построению временных массивов для каждого комментария.

Вопрос: Есть ли более простой способ обойти это?

<!-- This is the final html mark-up output: List of comments (threaded/nested) -->
<ul>
    <li id="1" class="push-<?php echo $push; ?> span-<?php echo $span; ?>">comment - parent</li>
    <li id="2">comment - child of #1
        <ul class="children">
            <li id="3">comment - child of #2
            <li id="4">comment - child of #2
                <ul class="children">
                    <li id="5">comment - child of #4</li>
                </ul>
            <li id="6">comment - child of #2</li>
        </ul>
        <li id="7">comment - child of #2
            <ul class="children">
                <li id="8">comment - child of #7</li>
                <li id="9">comment - child of #7</li>
            </ul>
        </li>
    </li>
</ul>

<?php
// This is my callback function
function comment_list_cb( $comment, $args, $depth )
{
    // retrieve the data from the globals or make them available
    $GLOBALS['comment'] = $comment;
    global $post;

    static $width = MY_GLOBAL_WIDTH_CONSTANT;
    static $ancestors = null;

    // is Child/Parent comment
    $parent = (int) $comment->comment_parent; // retrieve the ID of the parent

    $is_child = false;
    if ( $parent > (int) 0 ) // if we got a parent
    {
        $is_child = true;

        if ( ! (array) $ancestors )
            $ancestors = array();

        if ( ! array_key_exists( $parent, $ancestors ) )
        {
            $ancestors[$parent] = get_comment_ID();
        }
        else 
        {
            foreach ( $ancestors as $parent_id => $child_id )
            {
                if ( $parent_id == $parent )
                {
                    $ancestors_temp[$parent_id] = $child_id;
                    break;
                }

                $ancestors_temp[$parent_id] = $child_id;
            }
            $ancestors = $ancestors_temp;
        }

        $parent_counter = count( $ancestors );
        $span = $width - (int) $parent_counter;
    }
    else 
    {
        $ancestors = $parent_counter = null;
        $span = MY_GLOBAL_WIDTH_CONSTANT;
    }

$span_txt = $span - (int) 2; // reduce per `2` because of the span-2 class at the avatar element

    // now: build the classes
    $push = $parent_counter != (int) 0 ? 'push-1' : '';
    $child = $is_child === true ? ' child ' : '';
    $list = comment_class( 'span-'.$span.' '.$push.' append-bottom last hreview comment-'.get_comment_ID().' '.$microid, get_comment_ID(), $post->ID, false );

    ?>
    <!-- build the comment -->
    <li <?php echo $list; ?>>
        <div id="<?php get_comment_ID(); ?>">
            <span class="comment-avatar span-2"><!-- display avatar img - width is span-2 --></span>
            <span class="comment-meta span-<?php echo $span_txt; ?> last"><!-- display meta data like timestamp, etc. --></span>
            <span class="comment-text span-<?php echo $span_txt; ?> last"><!-- display message --></span>
        </div>
    </li>
    <?php
}

1 Ответ

1 голос
/ 11 мая 2011

Просто некоторые общие комментарии после быстрого сканирования.Он занимается кодированием независимо от его функциональности.

$GLOBALS['comment'] = $comment;

Почему вы ставите это в глобальном масштабе?Это может перезаписать существующую глобальную переменную.Переход по ссылке может быть более уместным здесь.

static $width = MY_GLOBAL_WIDTH_CONSTANT;

Почему это статично?Значение никогда не изменяется, поэтому нет необходимости сохранять.

if ( $parent > (int) 0 )
[...]
$span_txt = $span - (int) 2;
[...]
$push = $parent_counter != (int) 0 ? 'push-1' : '';

Нет необходимости приводить int к литеру int.Если вы хотите сравнить int, то это переменная, которую вы должны приводить.

if ( ! (array) $ancestors )
    $ancestors = array();

Если пустой массив, сделать пустой массив?Просто сделай !isset($ancestors)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...