__( 'Products', 'LDB_AP' ),
'singular_name' => __( 'Product', 'LDB_AP' ),
'add_new' => __( 'Add New', 'LDB_AP' ),
'add_new_item' => __( 'Add New Product', 'LDB_AP' ),
'edit_item' => __( 'Edit Product', 'LDB_AP' ),
'new_item' => __( 'New Product', 'LDB_AP' ),
'all_items' => __( 'All Products', 'LDB_AP' ),
'view_item' => __( 'View Product', 'LDB_AP' ),
'search_items' => __( 'Search Products', 'LDB_AP' ),
'not_found' => __( 'No products found', 'LDB_AP' ),
'not_found_in_trash' => __( 'No products found in Trash', 'LDB_AP' ),
'parent_item_colon' => '',
'menu_name' => __( 'Products', 'LDB_AP' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields' )
);
register_post_type( 'product', $args );
}
function AP_performAction() {
global $wpdb;
$action = $this->AP_currentAction();
switch( $action ) {
case 'process':
if( isset( $_REQUEST['feed'] ) && is_array( $_REQUEST['feed'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'bulk-feeds' ) ) {
foreach( $_REQUEST['feed'] as $feed ) {
$this->AP_processPrices( $feed );
}
$this->AP_setMessage( __( 'The feeds were processed succesfully.', 'LDB_AP' ), 'success' );
} else if( isset( $_GET['feed'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'process' . $_GET['feed'] ) ) {
$this->AP_processPrices( $_GET['feed'] );
$this->AP_setMessage( __( 'The feed was processed succesfully.', 'LDB_AP' ), 'success' );
} else {
wp_die( __( "You're not allowed to perform the requested action." ) );
}
break;
case 'delete':
if( isset( $_REQUEST['feed'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-feeds' ) ) {
$wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'apfeeds WHERE ID IN (' . implode( ', ', $_REQUEST['feed'] ) . ')' );
$this->AP_setMessage( __( 'The feeds were succesfully deleted.', 'LDB_AP' ), 'success' );
}
if( isset( $_REQUEST['feed'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete' . $_REQUEST['feed'] ) ) {
$wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'apfeeds WHERE id=' . $_GET['feed'] );
$this->AP_setMessage( __( 'The feed was succesfully deleted.', 'LDB_AP' ), 'success' );
}
break;
case 'draft':
if( isset( $_POST['identifier'] ) && isset( $_GET['feed'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'bulk-feeds' ) ) {
$items = @$this->AP_processFeed( $_GET['feed'] );
foreach( $_POST['identifier'] as $identifier ) {
foreach( $items as $item ) {
if( $item['identifier'] === $identifier )
if( $this->AP_createDraft( $item['name'], $item['matches'], $item['identifier'], $item['image'] ) )
$this->AP_setMessage( sprintf( __( 'The draft for %s was created succesfully.', 'LDB_AP' ), $item['name'] ), 'success' );
}
}
$feed = @$this->AP_getFeed( $_GET['feed'] );
$this->AP_processPrices( $_GET['feed'] );
$this->action = 'view';
} else if( isset( $_GET['identifier'] ) && isset( $_GET['feed'] ) && isset( $_GET['_draftnonce'] ) && wp_verify_nonce( $_GET['_draftnonce'], 'draft' . $_GET['identifier'] ) ) {
$items = @$this->AP_processFeed( $_GET['feed'] );
$post = '';
$postID = false;
$message = '';
$messagetype= '';
foreach( $items as $item ) {
if( $item['identifier'] === $_GET['identifier'] )
$postID = $this->AP_createDraft( $item['name'], $item['matches'], $item['identifier'], $item['image'] );
}
if( $postID && !is_array( $postID ) )
$post = '&post=' . $postID;
$feed = @$this->AP_getFeed( $_GET['feed'] );
$this->AP_processPrices( $_GET['feed'] );
$paged = '';
$orderby = '';
$order = '';
if( isset( $_GET['paged'] ) )
$paged = '&paged=' . $_GET['paged'];
if( isset( $_GET['orderby'] ) )
$orderby = '&orderby=' . $_GET['orderby'];
if( isset( $_GET['order'] ) )
$order = '&order=' . $_GET['order'];
header( 'Location:?page=' . $_GET['page'] . '&action=view&feed=' . $_GET['feed'] . '&_wpnonce=' . $_GET['_wpnonce'] . $paged . $orderby . $order . $post );
}
break;
default:
if( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['wp_nonce_add'] ) && wp_verify_nonce( $_POST['wp_nonce_add'], 'addfeed' ) ) {
$data = $this->AP_prepareFeedData();
$wpdb->insert( $wpdb->prefix . 'apfeeds', $data );
$this->AP_setMessage( __( 'The feed was succesfully saved.', 'LDB_AP' ), 'success' );
} else if( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['wp_nonce_edit'] ) && wp_verify_nonce( $_POST['wp_nonce_edit'], 'editfeed' ) ) {
$data = $this->AP_prepareFeedData();
$wpdb->update( $wpdb->prefix . 'apfeeds', $data, array( 'ID' => $_POST['ID'] ) );
$this->AP_setMessage( __( 'The feed was succesfully updated.', 'LDB_AP' ), 'success' );
}
break;
}
}
function AP_menu() {
$pages = array();
$pages[] = add_menu_page( __( 'Affiliate Press', 'LDB_AP' ), __( 'Affiliate Press', 'LDB_AP' ), 'manage_options', 'affiliate_press', array( &$this, 'AP_dashboard' ), LDB_AP_URL . 'images/icon16.png' );
add_submenu_page( 'affiliate_press', __( 'Dashboard', 'LDB_AP' ), __( 'Dashboard', 'LDB_AP' ), 'manage_options', 'affiliate_press' );
$pages[] = add_submenu_page( 'affiliate_press', __( 'Feeds', 'LDB_AP' ), __( 'Feeds', 'LDB_AP' ), 'manage_options', 'affiliate_press_feeds', array( &$this, 'AP_indexFeed' ) );
$pages[] = add_submenu_page( 'affiliate_press', __( 'Add New Feed', 'LDB_AP' ), __( 'Add New Feed', 'LDB_AP' ), 'manage_options', 'affiliate_press_add', array( &$this, 'AP_addFeed' ) );
foreach( $pages as $page )
add_action( 'admin_print_styles-' . $page, array( &$this, 'AP_loadStyle' ) );
}
function AP_addCustomMetaBox() {
add_meta_box( 'affiliate-press', __( 'Prices from feeds', 'LDB_AP' ), array( &$this, 'AP_pricesBox' ), 'product', 'side', 'default' );
}
function AP_dashboard() {
include( LDB_AP_VIEW_PATH . 'dashboard.php' );
}
function AP_addFeed() {
$this->action = 'add';
$this->AP_feeds();
}
function AP_indexFeed() {
if( !$this->AP_currentAction() ) {
$this->action = 'index';
}
$this->AP_feeds();
}
function AP_postbox( $id, $title, $content ) {
$postbox = '
' . $title . '
' . $content . '
';
echo $postbox;
}
function AP_sidebar() {
include( LDB_AP_VIEW_PATH . 'sidebar.php' );
}
function AP_pricesBox() {
global $post, $wpdb;
$productID = $post->ID;
$results = $wpdb->get_results( 'SELECT apfeeds.title, apfeeds.currency, apprices.price FROM ' . $wpdb->prefix . 'apprices apprices, ' . $wpdb->prefix . 'apfeeds apfeeds WHERE apprices.productID = ' . $productID . ' AND apprices.feedID = apfeeds.ID AND apprices.online = 1 ORDER BY apprices.price ASC' );
$pricetable = '';
if( $results ) {
$r = 1;
foreach( $results as $result ){
$class = 'normal';
if( count( $results ) === 1 ) {
$class = '';
} else if( $r === 1 ) {
$class = 'first';
} else if( $r === count( $results ) ) {
$class = 'last';
}
$pricetable.= '| ' . $result->title . ' | ' . $result->currency . ' ' . $result->price . ' |
';
$r++;
}
} else {
$pricetable.= '' . __( 'No prices were found.', 'LDB_AP' ) . ' | ';
}
$pricetable.= '
';
echo $pricetable;
}
function AP_currentAction() {
$action = false;
if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
$action = $_REQUEST['action'];
if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
$action = $_REQUEST['action2'];
if ( !$action && isset( $_GET['action'] ) )
$action = $_GET['action'];
return $action;
}
function AP_feeds() {
global $wpdb;
$action = $this->AP_currentAction();
if( $this->action ) {
$action = $this->action;
$this->action = false;
}
if( isset( $_GET['post'] ) ) {
$postinfo = get_post( $_GET['post'] );
$this->AP_setMessage( sprintf( __( 'The draft for %s was created succesfully.', 'LDB_AP' ), $postinfo->post_title ), 'success' );
}
switch( $action ) {
case 'view':
if( isset( $_GET['feed'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'view' . $_GET['feed'] ) ) {
$feed = @$this->AP_getFeed( $_GET['feed'] );
$items = @$this->AP_processFeed( $_GET['feed'] );
include( LDB_AP_VIEW_PATH . 'feed-view.php' );
} else {
wp_die( __( "You're not allowed to perform the requested action." ) );
}
break;
case 'add':
$matches = $this->AP_buildSelect( 'matches', $this->AP_getCustomFields() );
include( LDB_AP_VIEW_PATH . 'feed-add.php' );
break;
case 'edit':
if( isset( $_GET['feed'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'edit' . $_GET['feed'] ) ) {
$feed = @$this->AP_getFeed( $_REQUEST['feed'] );
$customfields = $this->AP_getCustomFields();
$matches = $this->AP_buildSelect( 'matches', $this->AP_getCustomFields(), $feed['matches'] );
include( LDB_AP_VIEW_PATH . 'feed-edit.php' );
} else {
wp_die( __( "You're not allowed to perform the requested action." ) );
}
break;
default:
include( LDB_AP_VIEW_PATH . 'feed-index.php' );
break;
}
}
function AP_catchAttachment( $att_id ){
$post = get_post( $att_id );
update_post_meta( $post->post_parent, '_thumbnail_id' , $att_id );
}
function AP_createDraft( $name, $matches, $identifier, $image ) {
$data = array(
'post_title' => $name,
'post_type' => 'product'
);
$post = wp_insert_post( $data );
if( $image && !empty( $image ) ) {
add_action( 'add_attachment', array( &$this, 'AP_catchAttachment' ) );
add_post_meta( $post, $matches, $identifier );
media_sideload_image( $image, $post );
remove_action( 'add_attachment', array( &$this, 'AP_catchAttachment' ) );
}
return $post;
}
function AP_prepareFeedData() {
$fields = array( 'title', 'currency', 'url', 'item_xpath', 'name_xpath', 'image_xpath', 'price_xpath', 'link_xpath', 'identifier_xpath', 'matches' );
$data = array();
foreach( $fields as $key => $value ) {
if( isset( $_POST[ $value ] ) ) {
$data[ $value ] = $_POST[ $value ];
} else {
$data[ $value ] = '';
}
}
if( isset( $_POST['new_matches'] ) && !empty( $_POST['new_matches'] ) ) {
$data['matches'] = $_POST['new_matches'];
}
return $data;
}
function AP_setMessage( $msg, $type ) {
$this->messages[] = array( $msg, $type );
}
function AP_getMessage() {
$messages = '';
foreach( $this->messages as $message ){
$messages.= '' . $message[0] . '
' . "\n";
}
$this->messages = array();
echo $messages;
}
function AP_getFeed( $id ) {
global $wpdb;
if( $id === null )
return false;
$feed = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'apfeeds WHERE id=' . $id, ARRAY_A );
return $feed;
}
function AP_getCustomFields() {
$fields = array();
$products = get_posts(
array(
'post_type' => 'product',
'post_status' => array( 'publish', 'draft' )
)
);
foreach( $products as $product ) {
$fields = array_merge( $fields, get_post_custom_keys( $product->ID ) );
}
foreach( $fields as $key => $field ) {
if( $field[0] === '_' )
unset( $fields[ $key ] );
}
$fields = array_unique( $fields );
return $fields;
}
function AP_buildSelect( $name, $values, $default = false ) {
$select = '';
return $select;
}
function AP_processFeed( $feed ) {
global $wpdb;
$info = json_decode( get_option( 'ap_feed_' . $feed, false ), true );
if( isset( $info['timestamp'] ) && $info['timestamp'] < ( time() - 300 ) ) {
return $info['data'];
} else {
$info = array();
$feedinfo = $this->AP_getFeed( $feed );
$data = wp_remote_get( $feedinfo['url'] );
$body = new DOMDocument();
@$body->loadXML( $data['body'] );
$xpath = new DOMXPath( $body );
$items = $xpath->query( $feedinfo['item_xpath'] );
foreach ( $items as $item ) {
$price = @$this->AP_getValue( @$xpath->query( $feedinfo['price_xpath'], $item ) );
$name = @$this->AP_getValue( @$xpath->query( $feedinfo['name_xpath'], $item ) );
$image = @$this->AP_getValue( @$xpath->query( $feedinfo['image_xpath'], $item ) );
$link = @$this->AP_getValue( @$xpath->query( $feedinfo['link_xpath'], $item ) );
$identifier = @$this->AP_getValue( @$xpath->query( $feedinfo['identifier_xpath'], $item ) );
$info[] = array(
'identifier' => $identifier,
'matches' => $feedinfo['matches'],
'currency' => $feedinfo['currency'],
'price' => $price,
'name' => $name,
'image' => $image,
'link' => $link,
'feed_id' => $feedinfo['ID']
);
}
$option = array(
'data' => $info,
'time' => time()
);
update_option( 'ap_feed_' . $feed, json_encode( $option ) );
return $info;
}
}
function AP_processPrices( $feed ) {
global $wpdb;
$info = array();
$feedinfo = $this->AP_getFeed( $feed );
$wpdb->query( 'UPDATE ' . $wpdb->prefix . 'apprices SET online = 0 WHERE feedID = ' . $feedinfo['ID'] );
$info = @$this->AP_processFeed( $feed );
foreach( $info as $item ) {
$query = 'SELECT wposts.ID FROM ' . $wpdb->posts . ' wposts, ' . $wpdb->postmeta . ' wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = "' . $item['matches'] . '" AND wpostmeta.meta_value = "' . $item['identifier'] . '" AND wposts.post_type = "product"';
$results = $wpdb->get_results( $query );
foreach( $results as $result ) {
$data = array(
'productID' => $result->ID,
'feedID' => $item['feed_id'],
'price' => $item['price'],
'link' => $item['link'],
'online' => 1
);
$update = $wpdb->get_row( 'SELECT * FROM ' . $wpdb->prefix . 'apprices WHERE productID = ' . $result->ID . ' AND feedID = ' . $item['feed_id'] );
if( $update ) {
$wpdb->update( $wpdb->prefix . 'apprices', $data, array( 'ID' => $update->ID ) );
} else {
$wpdb->insert( $wpdb->prefix . 'apprices', $data );
}
}
}
}
function AP_getValue( $e ) {
if( $e ) {
return trim( preg_replace( '/\s+/', ' ', @$e->item(0)->nodeValue ) );
} else {
return false;
}
}
function AP_buildTable( $data ) {
$table = '';
if( $data ) {
foreach( $data as $row ){
$table.= '| ' . $row->title . ' | ' . $row->price . ' |
';
}
} else {
$table.= '' . __( 'No prices were found.', 'LDB_AP' ) . ' | ';
}
$table.= '
';
return $table;
}
function AP_getPrices( $table = false ) {
global $post, $wpdb;
$results = $wpdb->get_results( 'SELECT apfeeds.title, apprices.price, apprices.link FROM ' . $wpdb->prefix . 'apprices apprices, ' . $wpdb->prefix . 'apfeeds apfeeds WHERE apprices.productID = ' . $post->ID . ' AND apprices.feedID = apfeeds.ID AND apprices.online = 1 ORDER BY apprices.price ASC' );
if ( $table )
$results = $this->AP_buildTable( $results );
return $results;
}
}
}