option_key );
$positions = $this->box_positions( $post_type );
wp_enqueue_style( 'awv_tester-style' );
wp_enqueue_script( 'awv_tester-script' );
?>
array(
'before_content' => array(
'title' => esc_html__( 'Before post content', 'awv-plugin' ),
'type' => 'filter',
'hook' => 'the_content',
'position' => 'before',
'priority' => '10',
'priority_ui' => false,
),
'after_content' => array(
'title' => esc_html__( 'After post content', 'awv-plugin' ),
'type' => 'filter',
'hook' => 'the_content',
'position' => 'after',
'priority' => '10',
'priority_ui' => false,
),
)
) );
return isset( $positions[ $post_type ] ) ? $positions[ $post_type ] : array();
}
/**
* Save form options
* @return viod
*/
public function update_options() {
if ( isset( $_POST[ $this->option_key ] ) && wp_verify_nonce( $_POST['awv_tester_nonce_fields'], 'awv_tester' ) ) {
$post_type = get_post_type();
$priorities = get_option( $this->option_key, array() );
$values = array_map( 'sanitize_text_field', $_POST[ $this->option_key ][ $post_type ] );
$priorities[ $post_type ] = $values;
update_option( $this->option_key, $priorities, false );
wp_redirect( get_the_permalink() );
}
}
/**
* Show possible box positions
* @return viod
*/
public function view_box_positions() {
$awv_options = get_option( 'awv_options' );
$enable_tester = isset( $awv_options['tester'] ) && $awv_options['tester'] == 1;
if ( is_singular('product') && is_user_logged_in() && $enable_tester ) {
$post_type = get_post_type();
$positions = $this->box_positions( $post_type );
$priorities = get_option( $this->option_key );
foreach ( $positions as $position_key => $position ) {
$priority = isset( $priorities[ $post_type ][ $position_key ] ) ? $priorities[ $post_type ][ $position_key ] : $position['priority'];
$title = esc_html( $position['title'] );
$title .= $position['priority_ui'] ? '(priority ' . esc_html( $priority ) . ')' : '';
$output = '' . $title . '
';
if ( $position['type'] == 'filter' ) {
add_filter( $position['hook'], function( $content ) use ( $output, $position ) {
return $this->combine_strings( $output, $position['position'], $content );
});
}
if ( $position['type'] == 'action' ) {
add_action( $position['hook'], function( $content ) use ( $output ) {
echo $output;
}, $priority );
}
}
}
}
/**
* Combine strings
* @param string $str1
* @param string $position
* @param string $str2
* @return string
*/
public function combine_strings( $str1, $position, $str2 ) {
if ( $position == 'before' ) {
return $str1 . $str2;
}
if ( $position == 'after' ) {
return $str2 . $str1;
}
}
public function tab_preview( $tabs ) {
$awv_options = get_option( 'awv_options' );
if ( isset( $awv_options['tester'] ) && $awv_options['tester'] == 1 ) {
$priorities = get_option( $this->option_key );
$priority = isset( $priorities['product']['woocommerce_product_tab'] ) ? $priorities['product']['woocommerce_product_tab'] : '10';
// Adds the new tab
$tabs['awv_tab'] = array(
'title' => esc_html__( 'Custom tab', 'awv-plugin' ),
'priority' => $priority,
'callback' => array($this, 'tab_preview_callback')
);
}
return $tabs;
}
public function tab_preview_callback() {
esc_html_e( 'Example of the tab content', 'awv-plugin' );
}
}
new AWV_Tester_Init();