Ejemplo n.º 1
0
/**
 * Custom rules for saving a track.
 *
 * @since 1.0.0
 * @todo Get ID3 info for remote files.
 *
 * @param int $post_id Post ID.
 */
function audiotheme_track_save_post($post_id)
{
    $is_autosave = defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ? true : false;
    $is_revision = wp_is_post_revision($post_id);
    $is_valid_nonce = isset($_POST['audiotheme_track_nonce']) && wp_verify_nonce($_POST['audiotheme_track_nonce'], 'update-track_' . $post_id) ? true : false;
    // Bail if the data shouldn't be saved or intention can't be verified.
    if ($is_autosave || $is_revision || !$is_valid_nonce) {
        return;
    }
    $track = get_post($post_id);
    $fields = array('artist', 'file_url', 'length', 'purchase_url');
    foreach ($fields as $field) {
        $value = empty($_POST[$field]) ? '' : $_POST[$field];
        if ('artist' === $field) {
            $value = sanitize_text_field($value);
        } elseif ('length' === $field) {
            $value = preg_replace('/[^0-9:]/', '', $value);
        } elseif (('file_url' === $field || 'purchase_url' === $field) && !empty($value)) {
            $value = esc_url_raw($value);
        }
        update_post_meta($post_id, '_audiotheme_' . $field, $value);
    }
    $is_downloadable = empty($_POST['is_downloadable']) ? null : 1;
    update_post_meta($post_id, '_audiotheme_is_downloadable', $is_downloadable);
    audiotheme_record_update_track_count($track->post_parent);
}
Ejemplo n.º 2
0
/**
 * Custom rules for saving a record.
 *
 * Creates and updates child tracks and saves additional record meta.
 *
 * @since 1.0.0
 *
 * @param int $post_id Post ID.
 */
function audiotheme_record_save_post($post_id)
{
    $is_autosave = defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ? true : false;
    $is_revision = wp_is_post_revision($post_id);
    $is_valid_nonce = isset($_POST['audiotheme_record_nonce']) && wp_verify_nonce($_POST['audiotheme_record_nonce'], 'update-record_' . $post_id) ? true : false;
    // Bail if the data shouldn't be saved or intention can't be verified.
    if ($is_autosave || $is_revision || !$is_valid_nonce) {
        return;
    }
    $current_user = wp_get_current_user();
    // Whitelisted fields.
    $fields = array('release_year', 'artist', 'genre');
    foreach ($fields as $field) {
        $value = empty($_POST[$field]) ? '' : $_POST[$field];
        update_post_meta($post_id, '_audiotheme_' . $field, $value);
    }
    // Update purchase urls.
    $record_links = array();
    if (isset($_POST['record_links']) && is_array($_POST['record_links'])) {
        foreach ($_POST['record_links'] as $link) {
            if (!empty($link['name']) && !empty($link['url'])) {
                $link['url'] = esc_url_raw($link['url']);
                $record_links[] = $link;
            }
        }
    }
    update_post_meta($post_id, '_audiotheme_record_links', $record_links);
    // Update tracklist.
    if (!empty($_POST['audiotheme_tracks'])) {
        $i = 1;
        foreach ($_POST['audiotheme_tracks'] as $track_data) {
            $default_data = array('artist' => '', 'post_id' => '', 'title' => '');
            $track_data = wp_parse_args($track_data, $default_data);
            $data = array();
            $track_id = empty($track_data['post_id']) ? '' : absint($track_data['post_id']);
            if (!empty($track_data['title'])) {
                $data['post_title'] = $track_data['title'];
                $data['post_status'] = 'publish';
                $data['post_parent'] = $post_id;
                $data['menu_order'] = $i;
                $data['post_type'] = 'audiotheme_track';
                // Insert or update track.
                if (empty($track_id)) {
                    $track_id = wp_insert_post($data);
                } else {
                    $data['ID'] = $track_id;
                    $data['post_author'] = $current_user->ID;
                    wp_update_post($data);
                }
                $i++;
            }
            // Update track artist and file url.
            if (!empty($track_id) && !is_wp_error($track_id)) {
                update_post_meta($track_id, '_audiotheme_artist', $track_data['artist']);
                update_post_meta($track_id, '_audiotheme_file_url', $track_data['file_url']);
            }
        }
        // Update track count.
        audiotheme_record_update_track_count($post_id);
    }
}