
function wpmmachine_get_ads( $page = 0, $per_page = 50 ) {
    global $wpdb;
    $page = max( 1, intval( $page ) + 1 );
    $offset = ( $page - 1 ) * $per_page;
    return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}moneymach_ads ORDER BY id DESC LIMIT %d, %d", $offset, $per_page ) );
}

function wpmmachine_get_ad( $ad_id ) {
    global $wpdb;
    return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}moneymach_ads WHERE id = %d", intval( $ad_id ) ) );
}

function wpmmachine_add_ad( $name, $type, $settings ) {
    global $wpdb;
    $now = current_time( 'mysql' );
    $wpdb->insert(
        $wpdb->prefix . 'moneymach_ads',
        array(
            'name' => $name,
            'type' => intval( $type ),
            'settings' => $settings,
            'created_at' => $now,
            'updated_at' => $now,
        ),
        array( '%s', '%d', '%s', '%s', '%s' )
    );
    return intval( $wpdb->insert_id );
}

function wpmmachine_update_ad( $ad_id, $name, $type, $settings ) {
    global $wpdb;
    return $wpdb->update(
        $wpdb->prefix . 'moneymach_ads',
        array(
            'name' => $name,
            'type' => intval( $type ),
            'settings' => $settings,
            'updated_at' => current_time( 'mysql' ),
        ),
        array( 'id' => intval( $ad_id ) ),
        array( '%s', '%d', '%s', '%s' ),
        array( '%d' )
    );
}

function wpmmachine_delete_ad( $ad_id ) {
    global $wpdb;
    return $wpdb->delete( $wpdb->prefix . 'moneymach_ads', array( 'id' => intval( $ad_id ) ), array( '%d' ) );
}

function wpmmachine_get_total_clicks() {
    global $wpdb;
    return intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}moneymach_clicks" ) );
}

function wpmmachine_track_click( $monetize_id = 0, $target_url = '', $source_url = '' ) {
    global $wpdb;
    return $wpdb->insert(
        $wpdb->prefix . 'moneymach_clicks',
        array(
            'monetize_id' => intval( $monetize_id ),
            'clicked_at' => current_time( 'mysql' ),
            'target_url' => $target_url,
            'source_url' => $source_url,
        ),
        array( '%d', '%s', '%s', '%s' )
    );
}
{"id":6,"count":20,"description":"","link":"https:\/\/barbaragancia.com.br\/bebereborn\/alfabeto\/b\/","name":"B","slug":"b","taxonomy":"alfabeto","parent":0,"meta":[],"_links":{"self":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/alfabeto\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/alfabeto"}],"about":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/taxonomies\/alfabeto"}]}}