The Stack Overflow podcast is back! Listen to an interview with our new CEO.
2 Fix code formatting, misc
source | link

I saw the question & answer to "How to get the number of times an audio file has been played" and I am looking for the same result, but I am not using the native wpWP audio player. I am using a custom theme that includes a custom player called scampScamp player.

This is whatswhat's in the themes phptheme's PHP for the player.:

if ( $panel_options && isset( $panel_options['scamp_player'] ) && $panel_options['scamp_player'] === 'on' ) {
    require_once( plugin_dir_path( __FILE__ ) . 'includes/post-types/scamp-player.php' );

HeresHere's the code from the original that I would like to use.:

add_action( 'wp_footer', function () {
    ?>
    <script>
    jQuery(document).ready(function() {
        (function ($) {
            var srcs = []; // Array of sources already sent to cut down on posts to server.
            $('audio.wp-audio-shortcode, .wp-audio-playlist audio').on('play', function (event) {
                // this.src should be the url (guid) of the audio file
                if (this.src && $.inArray(this.src, srcs) === -1) {
                    srcs.push(this.src);
                    $.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', {
                            action: 'wpse168584_audio_stats',
                            guid: this.src.replace(/\?.+$/, ''), // Remove any query vars.
                            nonce: '<?php echo wp_create_nonce( 'wpse168584_audio_stats_' ); ?>'
                        }, null, 'json'
                    );
                }
            });
        })(jQuery);
    });
    </script>
    <?php

add_action( 'wp_footer', function () { ?> jQuery(document).ready(function() { (function ($) { var srcs = []; // Array of sources already sent to cut down on posts to server. $('audio.wp-audio-shortcode, .wp-audio-playlist audio').on('play', function (event) { // this.src should be the url (guid) of the audio file if (this.src && $.inArray(this.src, srcs) === -1) { srcs.push(this.src); $.post( '', { action: 'wpse168584_audio_stats', guid: this.src.replace(/\?.+$/, ''), // Remove any query vars. nonce: '' }, null, 'json' ); } }); })(jQuery); }); false );

} );
function wpse168584_audio_stats() {
    $ret = array( 'error' => false );    
    if ( ! check_ajax_referer( 'wpse168584_audio_stats_', 'nonce', false /*die*/ ) ) {
        $ret['error'] = __( 'Permission error', 'wpfm' );
    } else {
        if ( ! isset( $_REQUEST['guid'] ) || ! ( $guid = $_REQUEST['guid'] ) ) {
            $ret['error'] = __( 'Params error', 'wpfm' );
        } else {
            global $wpdb;
            $sql = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE guid = %s LIMIT 1', $guid );
            if ( $post_id = $wpdb->get_var( $sql ) ) {
                // Use hex format to save space, 8 bytes for IPv4, 32 for IPv6.
                $ip = bin2hex( inet_pton( preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ) ) );
                if ( ! ( $meta = get_post_meta( $post_id, '_wp_attachment_metadata', true ) )
                || ! isset( $meta['plays_ips'] )
                || ! in_array( $ip, $plays_ips = explode( ';', $meta['plays_ips'] ) ) ) {
                    $plays_ips[] = $ip;
                    // If data getting too big, drop off oldest ip (FIFO).
                    if ( strlen( $meta['play_ids'] ) > 1000 ) array_shift( $plays_ips );
                    // Save as string to save space.
                    $meta['plays_ips'] = implode( ';', $plays_ips );
                    $meta['plays'] = isset( $meta['plays'] ) ? $meta['plays'] + 1 : 1;
                    update_post_meta( $post_id, '_wp_attachment_metadata', $meta );
                }
            }
        }
    }
     
    wp_send_json( $ret );
}

      
add_action( 'wp_ajax_nopriv_wpse168584_audio_stats', 'wpse168584_audio_stats' );
add_action( 'wp_ajax_wpse168584_audio_stats', 'wpse168584_audio_stats' );

add_filter( 'media_submitbox_misc_sections', function ( $arr ) {
    $arr['plays'] = __( 'Play Count:' );
    return $arr;
} );

How would I get the same results using this scampScamp player?

I saw the question & answer to "How to get the number of times an audio file has been played" and I am looking for the same result but I am not using the native wp audio player. I am using a custom theme that includes a custom player called scamp player.

This is whats in the themes php for the player.

if ( $panel_options && isset( $panel_options['scamp_player'] ) && $panel_options['scamp_player'] === 'on' ) {
require_once( plugin_dir_path( __FILE__ ) . 'includes/post-types/scamp-player.php' );

Heres the code from the original that I would like to use.

    <?php

add_action( 'wp_footer', function () { ?> jQuery(document).ready(function() { (function ($) { var srcs = []; // Array of sources already sent to cut down on posts to server. $('audio.wp-audio-shortcode, .wp-audio-playlist audio').on('play', function (event) { // this.src should be the url (guid) of the audio file if (this.src && $.inArray(this.src, srcs) === -1) { srcs.push(this.src); $.post( '', { action: 'wpse168584_audio_stats', guid: this.src.replace(/\?.+$/, ''), // Remove any query vars. nonce: '' }, null, 'json' ); } }); })(jQuery); }); false );

if ( ! check_ajax_referer( 'wpse168584_audio_stats_', 'nonce', false /*die*/ ) ) {
    $ret['error'] = __( 'Permission error', 'wpfm' );
} else {
    if ( ! isset( $_REQUEST['guid'] ) || ! ( $guid = $_REQUEST['guid'] ) ) {
        $ret['error'] = __( 'Params error', 'wpfm' );
    } else {
        global $wpdb;
        $sql = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE guid = %s LIMIT 1', $guid );
        if ( $post_id = $wpdb->get_var( $sql ) ) {
            // Use hex format to save space, 8 bytes for IPv4, 32 for IPv6.
            $ip = bin2hex( inet_pton( preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ) ) );
            if ( ! ( $meta = get_post_meta( $post_id, '_wp_attachment_metadata', true ) )
            || ! isset( $meta['plays_ips'] )
            || ! in_array( $ip, $plays_ips = explode( ';', $meta['plays_ips'] ) ) ) {
                $plays_ips[] = $ip;
                // If data getting too big, drop off oldest ip (FIFO).
                if ( strlen( $meta['play_ids'] ) > 1000 ) array_shift( $plays_ips );
                // Save as string to save space.
                $meta['plays_ips'] = implode( ';', $plays_ips );
                $meta['plays'] = isset( $meta['plays'] ) ? $meta['plays'] + 1 : 1;
                update_post_meta( $post_id, '_wp_attachment_metadata', $meta );
            }
        }
    }
}
 
wp_send_json( $ret );
}

   add_action( 'wp_ajax_nopriv_wpse168584_audio_stats', 'wpse168584_audio_stats' );
add_action( 'wp_ajax_wpse168584_audio_stats', 'wpse168584_audio_stats' );

add_filter( 'media_submitbox_misc_sections', function ( $arr ) {
$arr['plays'] = __( 'Play Count:' );
return $arr;
} );

How would I get the same results using this scamp player?

I saw the question & answer to "How to get the number of times an audio file has been played" and I am looking for the same result, but I am not using the native WP audio player. I am using a custom theme that includes a custom player called Scamp player.

This is what's in the theme's PHP for the player:

if ( $panel_options && isset( $panel_options['scamp_player'] ) && $panel_options['scamp_player'] === 'on' ) {
    require_once( plugin_dir_path( __FILE__ ) . 'includes/post-types/scamp-player.php' );

Here's the code from the original that I would like to use:

add_action( 'wp_footer', function () {
    ?>
    <script>
    jQuery(document).ready(function() {
        (function ($) {
            var srcs = []; // Array of sources already sent to cut down on posts to server.
            $('audio.wp-audio-shortcode, .wp-audio-playlist audio').on('play', function (event) {
                // this.src should be the url (guid) of the audio file
                if (this.src && $.inArray(this.src, srcs) === -1) {
                    srcs.push(this.src);
                    $.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', {
                            action: 'wpse168584_audio_stats',
                            guid: this.src.replace(/\?.+$/, ''), // Remove any query vars.
                            nonce: '<?php echo wp_create_nonce( 'wpse168584_audio_stats_' ); ?>'
                        }, null, 'json'
                    );
                }
            });
        })(jQuery);
    });
    </script>
    <?php
} );
function wpse168584_audio_stats() {
    $ret = array( 'error' => false );    
    if ( ! check_ajax_referer( 'wpse168584_audio_stats_', 'nonce', false /*die*/ ) ) {
        $ret['error'] = __( 'Permission error', 'wpfm' );
    } else {
        if ( ! isset( $_REQUEST['guid'] ) || ! ( $guid = $_REQUEST['guid'] ) ) {
            $ret['error'] = __( 'Params error', 'wpfm' );
        } else {
            global $wpdb;
            $sql = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE guid = %s LIMIT 1', $guid );
            if ( $post_id = $wpdb->get_var( $sql ) ) {
                // Use hex format to save space, 8 bytes for IPv4, 32 for IPv6.
                $ip = bin2hex( inet_pton( preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ) ) );
                if ( ! ( $meta = get_post_meta( $post_id, '_wp_attachment_metadata', true ) )
                || ! isset( $meta['plays_ips'] )
                || ! in_array( $ip, $plays_ips = explode( ';', $meta['plays_ips'] ) ) ) {
                    $plays_ips[] = $ip;
                    // If data getting too big, drop off oldest ip (FIFO).
                    if ( strlen( $meta['play_ids'] ) > 1000 ) array_shift( $plays_ips );
                    // Save as string to save space.
                    $meta['plays_ips'] = implode( ';', $plays_ips );
                    $meta['plays'] = isset( $meta['plays'] ) ? $meta['plays'] + 1 : 1;
                    update_post_meta( $post_id, '_wp_attachment_metadata', $meta );
                }
            }
        }
    }    
    wp_send_json( $ret );
}     
add_action( 'wp_ajax_nopriv_wpse168584_audio_stats', 'wpse168584_audio_stats' );
add_action( 'wp_ajax_wpse168584_audio_stats', 'wpse168584_audio_stats' );

add_filter( 'media_submitbox_misc_sections', function ( $arr ) {
    $arr['plays'] = __( 'Play Count:' );
    return $arr;
} );

How would I get the same results using this Scamp player?

1
source | link

Number of times an audio file has been played using custom player

I saw the question & answer to "How to get the number of times an audio file has been played" and I am looking for the same result but I am not using the native wp audio player. I am using a custom theme that includes a custom player called scamp player.

This is whats in the themes php for the player.

if ( $panel_options && isset( $panel_options['scamp_player'] ) && $panel_options['scamp_player'] === 'on' ) {
require_once( plugin_dir_path( __FILE__ ) . 'includes/post-types/scamp-player.php' );

Heres the code from the original that I would like to use.

    <?php

add_action( 'wp_footer', function () { ?> jQuery(document).ready(function() { (function ($) { var srcs = []; // Array of sources already sent to cut down on posts to server. $('audio.wp-audio-shortcode, .wp-audio-playlist audio').on('play', function (event) { // this.src should be the url (guid) of the audio file if (this.src && $.inArray(this.src, srcs) === -1) { srcs.push(this.src); $.post( '', { action: 'wpse168584_audio_stats', guid: this.src.replace(/\?.+$/, ''), // Remove any query vars. nonce: '' }, null, 'json' ); } }); })(jQuery); }); false );

if ( ! check_ajax_referer( 'wpse168584_audio_stats_', 'nonce', false /*die*/ ) ) {
    $ret['error'] = __( 'Permission error', 'wpfm' );
} else {
    if ( ! isset( $_REQUEST['guid'] ) || ! ( $guid = $_REQUEST['guid'] ) ) {
        $ret['error'] = __( 'Params error', 'wpfm' );
    } else {
        global $wpdb;
        $sql = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE guid = %s LIMIT 1', $guid );
        if ( $post_id = $wpdb->get_var( $sql ) ) {
            // Use hex format to save space, 8 bytes for IPv4, 32 for IPv6.
            $ip = bin2hex( inet_pton( preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ) ) );
            if ( ! ( $meta = get_post_meta( $post_id, '_wp_attachment_metadata', true ) )
            || ! isset( $meta['plays_ips'] )
            || ! in_array( $ip, $plays_ips = explode( ';', $meta['plays_ips'] ) ) ) {
                $plays_ips[] = $ip;
                // If data getting too big, drop off oldest ip (FIFO).
                if ( strlen( $meta['play_ids'] ) > 1000 ) array_shift( $plays_ips );
                // Save as string to save space.
                $meta['plays_ips'] = implode( ';', $plays_ips );
                $meta['plays'] = isset( $meta['plays'] ) ? $meta['plays'] + 1 : 1;
                update_post_meta( $post_id, '_wp_attachment_metadata', $meta );
            }
        }
    }
}

wp_send_json( $ret );
}

  add_action( 'wp_ajax_nopriv_wpse168584_audio_stats', 'wpse168584_audio_stats' );
add_action( 'wp_ajax_wpse168584_audio_stats', 'wpse168584_audio_stats' );

add_filter( 'media_submitbox_misc_sections', function ( $arr ) {
$arr['plays'] = __( 'Play Count:' );
return $arr;
} );

How would I get the same results using this scamp player?