Blog

  • craft_pe_purchases.csv

    lifetime saver (WordPress / Code Snippets).
    *
    * Code Snippets me paste karte waqt: snippet type = PHP, “Run snippet everywhere”.
    * Pehla “ 300,
    ‘display’ => ‘Every 5 Minutes (Craft.PE)’,
    );
    return $schedules;
    } );

    add_action( ‘init’, function () {
    $ev = wp_get_scheduled_event( CRAFT_PE_HOOK );
    // purana 30-minute wala schedule ho to hata do
    if ( $ev && $ev->schedule !== ‘craft_pe_five_min’ ) {
    wp_clear_scheduled_hook( CRAFT_PE_HOOK );
    $ev = false;
    }
    if ( ! $ev ) {
    wp_schedule_event( time() + 60, ‘craft_pe_five_min’, CRAFT_PE_HOOK );
    }
    } );

    // 2. Table (hamesha ke liye history)
    function craft_pe_table() {
    global $wpdb;
    return $wpdb->prefix . ‘craft_pe_purchases’;
    }

    add_action( ‘init’, function () {
    if ( get_option( ‘craft_pe_db_version’ ) === ‘1’ ) {
    return;
    }
    global $wpdb;
    require_once ABSPATH . ‘wp-admin/includes/upgrade.php’;
    $table = craft_pe_table();
    $charset = $wpdb->get_charset_collate();
    dbDelta( “CREATE TABLE $table (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    hash CHAR(64) NOT NULL,
    buyer VARCHAR(191) NOT NULL,
    item VARCHAR(191) NOT NULL,
    purchase_date VARCHAR(10) NOT NULL,
    purchase_time VARCHAR(5) NOT NULL,
    scraped_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY hash (hash)
    ) $charset;” );
    update_option( ‘craft_pe_db_version’, ‘1’ );
    } );

    // 3. Sirf “Последние покупки” block parse karo
    function craft_pe_parse( $html ) {
    $html = preg_replace( ‘#<(script|style|noscript)\b.*?#is’, ”, $html );
    $html = preg_replace( ‘/<[^>]+>/’, “\n”, $html );
    $html = html_entity_decode( $html, ENT_QUOTES | ENT_HTML5, ‘UTF-8’ );

    $lines = array_filter( array_map( ‘trim’, explode( “\n”, $html ) ), ‘strlen’ );
    $text = implode( “\n”, $lines );

    $start = false;
    foreach ( array( ‘Последние покупки’, ‘Recent purchases’, ‘Latest purchases’ ) as $h ) {
    $pos = mb_strpos( $text, $h );
    if ( false !== $pos ) {
    $start = $pos + mb_strlen( $h );
    break;
    }
    }
    if ( false === $start ) {
    return array();
    }
    $block = mb_substr( $text, $start );

    // footer par ruk jao taake extra saman na aaye
    foreach ( array( ‘© ModsCraft’, ‘Навигация’, ‘Этот проект не является’ ) as $stop ) {
    $p = mb_strpos( $block, $stop );
    if ( false !== $p ) {
    $block = mb_substr( $block, 0, $p );
    }
    }

    $rows = array();
    if ( preg_match_all(
    ‘/^(?P[^\n]+)\n(?:купил\(а\)|bought|purchased)\s+(?P[^\n]+)\n(?P\d{2}\.\d{2})\n(?P

    // 4. Scrape + sab nayi entries ek hi query me save
    function craft_pe_scrape() {
    global $wpdb;
    $res = wp_remote_get( CRAFT_PE_URL, array(
    ‘timeout’ => 30,
    ‘user-agent’ => ‘Mozilla/5.0 (compatible; recent-buyers-saver/1.0)’,
    ) );
    if ( is_wp_error( $res ) || 200 !== wp_remote_retrieve_response_code( $res ) ) {
    return array( ‘parsed’ => 0, ‘new’ => 0, ‘error’ => ‘fetch failed’ );
    }
    $rows = craft_pe_parse( wp_remote_retrieve_body( $res ) );
    if ( ! $rows ) {
    return array( ‘parsed’ => 0, ‘new’ => 0, ‘error’ => ‘no purchases parsed’ );
    }

    $table = craft_pe_table();
    $rows = array_reverse( $rows ); // purani pehle, nayi baad me
    $now = current_time( ‘mysql’ );
    $values = array();
    foreach ( $rows as $r ) {
    $values[] = $wpdb->prepare( ‘(%s,%s,%s,%s,%s,%s)’, $r[‘hash’], $r[‘buyer’], $r[‘item’], $r[‘date’], $r[‘time’], $now );
    }
    $before = (int) $wpdb->get_var( “SELECT COUNT(*) FROM $table” );
    // INSERT IGNORE + UNIQUE hash = duplicate kabhi dobara save nahi hota
    $wpdb->query( “INSERT IGNORE INTO $table (hash,buyer,item,purchase_date,purchase_time,scraped_at) VALUES ” . implode( ‘,’, $values ) );
    $after = (int) $wpdb->get_var( “SELECT COUNT(*) FROM $table” );

    return array( ‘parsed’ => count( $rows ), ‘new’ => $after – $before, ‘error’ => ” );
    }
    add_action( CRAFT_PE_HOOK, ‘craft_pe_scrape’ );

    // 5. Admin page: saved purchases dekhne ke liye + “Abhi chalao” button
    add_action( ‘admin_menu’, function () {
    add_menu_page( ‘Craft.PE Purchases’, ‘Craft.PE Purchases’, ‘manage_options’, ‘craft-pe-purchases’, ‘craft_pe_admin_page’, ‘dashicons-cart’, 58 );
    } );

    // CSV download (Excel me seedha khulti hai)
    add_action( ‘admin_init’, function () {
    if ( ! isset( $_GET[‘craft_pe_csv’] ) || ! current_user_can( ‘manage_options’ ) ) {
    return;
    }
    check_admin_referer( ‘craft_pe_csv_dl’ );
    global $wpdb;
    $rows = $wpdb->get_results(
    ‘SELECT id,buyer,item,purchase_date,purchase_time,scraped_at FROM ‘ . craft_pe_table() . ‘ ORDER BY id ASC’,
    ARRAY_A
    );
    nocache_headers();
    header( ‘Content-Type: text/csv; charset=utf-8’ );
    header( ‘Content-Disposition: attachment; filename=craft-pe-purchases-‘ . gmdate( ‘Y-m-d’ ) . ‘.csv’ );
    $out = fopen( ‘php://output’, ‘w’ );
    fwrite( $out, “\xEF\xBB\xBF” ); // UTF-8 BOM taake Excel me Russian text theek dikhe
    fputcsv( $out, array( ‘ID’, ‘Buyer’, ‘Item’, ‘Date’, ‘Time’, ‘Saved at’ ) );
    foreach ( $rows as $r ) {
    // Excel formula injection se bachao
    foreach ( array( ‘buyer’, ‘item’ ) as $f ) {
    if ( preg_match( ‘/^[=+\-@]/’, $r[ $f ] ) ) {
    $r[ $f ] = “‘” . $r[ $f ];
    }
    }
    fputcsv( $out, $r );
    }
    fclose( $out );
    exit;
    } );

    function craft_pe_admin_page() {
    global $wpdb;
    $table = craft_pe_table();

    if ( isset( $_POST[‘craft_pe_run’] ) && check_admin_referer( ‘craft_pe_run_now’ ) ) {
    $r = craft_pe_scrape();
    echo ‘

    Parsed: ‘ . (int) $r[‘parsed’] . ‘ | Naye saved: ‘ . (int) $r[‘new’] . ( $r[‘error’] ? ‘ | ‘ . esc_html( $r[‘error’] ) : ” ) . ‘

    ‘;
    }

    $total = (int) $wpdb->get_var( “SELECT COUNT(*) FROM $table” );
    $rows = $wpdb->get_results( “SELECT * FROM $table ORDER BY id DESC LIMIT 300” );

    echo ‘

    Craft.PE Recent Purchases

    ‘;
    echo ‘

    Total saved: ‘ . $total . ‘

    ‘;
    echo ‘

    ‘;
    wp_nonce_field( ‘craft_pe_run_now’ );
    echo ‘

    ‘;
    echo ‘Download CSV

    ‘;
    echo ‘

    ‘;
    foreach ( $rows as $r ) {
    printf(
    ‘

    ‘,
    (int) $r->id,
    esc_html( $r->buyer ),
    esc_html( $r->item ),
    esc_html( $r->purchase_date ),
    esc_html( $r->purchase_time ),
    esc_html( $r->scraped_at )
    );
    }
    echo ‘

    # Buyer Item Date Time Saved at
    %d %s %s %s %s %s

    ‘;
    }

  • Kk