
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":14,"date":"2025-03-21T15:58:23","date_gmt":"2025-03-21T15:58:23","guid":{"rendered":"https:\/\/barbaragancia.com.br\/bebereborn\/?page_id=14"},"modified":"2025-03-21T15:58:23","modified_gmt":"2025-03-21T15:58:23","slug":"contato","status":"publish","type":"page","link":"https:\/\/barbaragancia.com.br\/bebereborn\/contato\/","title":{"rendered":"CONTATO"},"content":{"rendered":"<h2><strong>ENVIE UM E-MAIL PARA ENTRAR EM CONTATO:<\/strong><\/h2>\n<p><span class=\"sigijh_hlt\">E-MAIL: R7COSTA00@GMAIL.COM<\/span><\/p>\n<p>RESPONDEREMOS O MAIS BREVE POSS\u00cdVEL<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ENVIE UM E-MAIL PARA ENTRAR EM CONTATO: E-MAIL: R7COSTA00@GMAIL.COM RESPONDEREMOS O MAIS BREVE POSS\u00cdVEL<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-14","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/pages\/14","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=14"}],"version-history":[{"count":2,"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/pages\/14\/revisions"}],"predecessor-version":[{"id":16,"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/pages\/14\/revisions\/16"}],"wp:attachment":[{"href":"https:\/\/barbaragancia.com.br\/bebereborn\/wp-json\/wp\/v2\/media?parent=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}