...

). * @param array $config The 'repeat_config'- object. * * @return array The array of indices on which we should inject. */ public function calculate_injection_indices( array $targets, array $config ): array { $this->config = wp_parse_args( $config, [ 'enabled' => false, 'offset' => 0, 'limit' => 0, 'min_words' => 0, 'repeat_mode' => 'manual', 'every' => 1, 'density' => 'mid', ] ); $from_bottom = ! empty( $this->config['from_bottom'] ); $offset = (int) $this->config['offset']; $total_targets = count( $targets ); if ( ! $this->config['enabled'] ) { if ( $from_bottom ) { $index = $total_targets - 1 - $offset; } else { $index = $offset; } return isset( $targets[ $index ] ) ? [ $index ] : []; } $from_bottom = ! empty( $config['from_bottom'] ); if ( $from_bottom ) { $targets = array_values( array_reverse( $targets ) ); } $indices_to_inject = []; $total_targets = count( $targets ); $every = $this->get_every_value( $total_targets, $this->config ); if ( isset( $targets[ $offset ] ) ) { $indices_to_inject[] = $offset; $this->injected_count++; } // Start the loop after the offset. $start_index = $offset + 1; for ( $index = $start_index; $index < $total_targets; $index++ ) { $target_html = $targets[ $index ]; $this->item_count_since_last++; /*[adpresso_fs()->can_use_premium_code()]*/ if ( ! empty( $config['min_words'] ) ) { $this->count_words( $target_html ); } /*[adpresso_fs()->can_use_premium_code()]*/ $ready_by_count = ( $this->item_count_since_last >= $every ); $ready_by_words = true; /*[adpresso_fs()->can_use_premium_code()]*/ if ( ! empty( $config['min_words'] ) && $this->word_count_since_last < $config['min_words'] ) { $ready_by_words = false; } if ( $ready_by_count && $ready_by_words ) { // Limit-Check if ( $this->config['limit'] > 0 && $this->injected_count >= $this->config['limit'] ) { break; // Global limit has been reached for this repetition. } // Injection! $indices_to_inject[] = $index; // Reset counters. $this->injected_count++; $this->item_count_since_last = 0; $this->word_count_since_last = 0; } } if ( $from_bottom ) { $original_indices = []; $max_index = $total_targets - 1; foreach ( $indices_to_inject as $reversed_index ) { $original_indices[] = $max_index - $reversed_index; } sort( $original_indices ); return $original_indices; } return $indices_to_inject; } /** * Count the words, but only when the function is needed from Pro. * * @param string $html * * @return void */ private function count_words( string $html ): void { $this->word_count_since_last += str_word_count( wp_strip_all_tags( $html ) ); } /** * Get the "every" value for the repeat strategy. * * @param int $total_items * @param $config * * @return int */ private function get_every_value( int $total_items, $config ): int { /*[adpresso_fs()->can_use_premium_code()]*/ if ( $config['repeat_mode'] === 'auto' ) { switch ( $config['density'] ?? 'mid' ) { case 'high': return max( 1, (int) floor( $total_items / 6 ) ); case 'low': return max( 1, (int) floor( $total_items / 3 ) ); default: return max( 1, (int) floor( $total_items / 4 ) ); } } /*[/adpresso_fs()->can_use_premium_code()]*/ return max( 1, (int) $config['every'] ); } }