
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":39,"date":"2025-05-22T22:06:09","date_gmt":"2025-05-22T22:06:09","guid":{"rendered":"https:\/\/barbaragancia.com.br\/bebereborn\/?page_id=39"},"modified":"2025-05-22T22:06:29","modified_gmt":"2025-05-22T22:06:29","slug":"dicionario-a-z","status":"publish","type":"page","link":"https:\/\/barbaragancia.com.br\/bebereborn\/dicionario-a-z\/","title":{"rendered":"DICIONARIO A-Z"},"content":{"rendered":"\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"template-page-glossario.php","meta":{"footnotes":""},"class_list":["post-39","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/pages\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":1,"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/pages\/39\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/pages\/39\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/media?parent=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}