ID, 'affiliate_image_url', true));
$product_title = sanitize_text_field(get_post_meta($post->ID, 'affiliate_product_title', true));
$amazon_url = esc_url_raw(get_post_meta($post->ID, 'affiliate_amazon_url', true));
$price = sanitize_text_field(get_post_meta($post->ID, 'affiliate_price', true));
$show_prime_logo = sanitize_text_field(get_post_meta($post->ID, 'affiliate_show_prime_logo', true));
$show_bestseller_label = sanitize_text_field(get_post_meta($post->ID, 'affiliate_show_bestseller_label', true));
$selected_market = sanitize_text_field(get_post_meta($post->ID, 'affiliate_amazon_market', true)) ?: 'de';
// Adjust button text and affiliate ID based on selected market
$button_text = sanitize_text_field(get_option('affiliate_button_text_' . $selected_market, __('Buy on Amazon', 'affiliamaze-amazon-affiliate-linker')));
$affiliate_id = sanitize_text_field(get_option('affiliate_' . $selected_market . '_id'));
$affiliate_link = esc_url($amazon_url . '?tag=' . $affiliate_id);
// Metabox HTML for entering product information
echo '';
echo '';
echo ''; // Text domain added here
echo '';
echo ''; // Text domain added here
echo '';
echo ''; // Text domain added here
echo '';
// View Amazon URL with Affiliate ID
echo ''; // Text domain added here
echo '';
echo ''; // Text domain added here
echo '';
// Add read-only field for button text
echo ''; // Text domain added here
echo '';
echo ''; // Text domain added here
echo '';
echo ''; // Text domain added here
echo '';
// Add Bestseller label option
echo ''; // Text domain added here
echo '';
}
// Sanitization, validation, and escaping for nonces
function affiliate_linker_save_affiliate_product_meta_box($post_id) {
// Check if the nonce is set and verified
if (!isset($_POST['affiliate_product_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['affiliate_product_nonce'])), 'affiliate_product_nonce')) {
return;
}
// Check if the user has permission to edit the post
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Save selected market choice
if (isset($_POST['affiliate_amazon_market'])) {
update_post_meta($post_id, 'affiliate_amazon_market', sanitize_text_field($_POST['affiliate_amazon_market']));
// Assign corresponding affiliate ID based on market choice
$market_code = sanitize_text_field($_POST['affiliate_amazon_market']);
$affiliate_id = sanitize_text_field(get_option('affiliate_' . $market_code . '_id'));
update_post_meta($post_id, 'affiliate_amazon_id', $affiliate_id);
}
// Check for empty fields except price
$required_fields = ['affiliate_image_url', 'affiliate_product_title', 'affiliate_amazon_url', 'affiliate_amazon_market', 'affiliate_show_prime_logo'];
foreach ($required_fields as $field) {
if (!isset($_POST[$field]) || trim($_POST[$field]) === '') {
// Add error message or other actions
return; // Stops saving if any required field is empty
}
}
// Save metadata
update_post_meta($post_id, 'affiliate_image_url', sanitize_text_field($_POST['affiliate_image_url']));
update_post_meta($post_id, 'affiliate_product_title', sanitize_text_field($_POST['affiliate_product_title']));
update_post_meta($post_id, 'affiliate_amazon_url', esc_url_raw($_POST['affiliate_amazon_url']));
update_post_meta($post_id, 'affiliate_price', sanitize_text_field($_POST['affiliate_price']));
update_post_meta($post_id, 'affiliate_show_prime_logo', sanitize_text_field($_POST['affiliate_show_prime_logo']));
// Save Bestseller label setting
if (isset($_POST['affiliate_show_bestseller_label'])) {
update_post_meta($post_id, 'affiliate_show_bestseller_label', sanitize_text_field($_POST['affiliate_show_bestseller_label']));
}
}
add_action('save_post', 'affiliate_linker_save_affiliate_product_meta_box');
// Add JavaScript and CSS
function affiliate_linker_enqueue_scripts() {
wp_enqueue_script('affiliate-linker-script', plugin_dir_url(__FILE__) . 'js/affiliate-linker.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'affiliate_linker_enqueue_scripts');
function affiliate_linker_enqueue_styles() {
wp_enqueue_style('affiliate-linker-style', plugin_dir_url(__FILE__) . 'css/affiliate-linker.css', array(), '1.0.0', 'all');
}
add_action('wp_enqueue_scripts', 'affiliate_linker_enqueue_styles');
function affiliate_linker_enqueue_admin_scripts() {
wp_enqueue_script('affiliate-linker-admin-script', plugin_dir_url(__FILE__) . 'js/admin-affiliate-linker.js', array('jquery'), '1.0.0', true);
wp_enqueue_style('affiliate-linker-admin-style', plugin_dir_url(__FILE__) . 'css/admin-affiliate-linker.css', array(), '1.0.0', 'all');
}
add_action('admin_enqueue_scripts', 'affiliate_linker_enqueue_admin_scripts');
// Custom Post Type for affiliate products and add it to admin menu
function affiliate_linker_create_affiliate_product_cpt_and_menu() {
$args = array(
'public' => true,
'label' => __('Affiliate Products', 'affiliamaze-amazon-affiliate-linker'),
'supports' => array('title'),
);
register_post_type('affiliate_product', $args);
// Add parent menu
add_menu_page(
__('AffiliAmaze', 'affiliamaze-amazon-affiliate-linker'), // Updated to "AffiliAmaze"
__('AffiliAmaze', 'affiliamaze-amazon-affiliate-linker'), // Updated to "AffiliAmaze"
'manage_options',
'affiliamaze-info', // Updated to 'affiliamaze-info' for the Info submenu
'affiliate_linker_render_info_page', // Updated callback function
'dashicons-admin-links'
);
// Adding the Info submenu
add_submenu_page(
'affiliamaze-info', // Parent slug
__('Info', 'affiliamaze-amazon-affiliate-linker'), // Menu title
__('Info', 'affiliamaze-amazon-affiliate-linker'), // Menu title
'manage_options', // Capability
'affiliamaze-info', // Menu slug
'affiliate_linker_render_info_page' // Callback function
);
// Adding the Affiliate Products submenu
add_submenu_page(
'affiliamaze-info', // Parent slug
__('Affiliate Products', 'affiliamaze-amazon-affiliate-linker'), // Menu title
__('Affiliate Products', 'affiliamaze-amazon-affiliate-linker'), // Menu title
'manage_options', // Capability
'edit.php?post_type=affiliate_product' // Menu slug
);
// Adding the Settings submenu
add_submenu_page(
'affiliamaze-info', // Parent slug
__('Settings', 'affiliamaze-amazon-affiliate-linker'), // Menu title
__('Settings', 'affiliamaze-amazon-affiliate-linker'), // Menu title
'manage_options', // Capability
'affiliate-link-settings', // Menu slug
'affiliate_linker_affiliate_link_settings_submenu' // Callback function
);
// Adding the Import JSON submenu
add_submenu_page(
'affiliamaze-info', // Parent slug
esc_html__('Import JSON', 'affiliamaze-amazon-affiliate-linker'),
esc_html__('Import JSON', 'affiliamaze-amazon-affiliate-linker'),
'manage_options',
'affiliate-products-import',
'affiliate_linker_affiliate_products_import_page'
);
// Adding the Export JSON submenu
add_submenu_page(
'affiliamaze-info', // Parent slug
esc_html__('Export JSON', 'affiliamaze-amazon-affiliate-linker'),
esc_html__('Export JSON', 'affiliamaze-amazon-affiliate-linker'),
'manage_options',
'export-json',
'affiliate_linker_render_export_json_page'
);
}
add_action('admin_menu', 'affiliate_linker_create_affiliate_product_cpt_and_menu');
// Function to render the information page
function affiliate_linker_render_info_page() {
?>
AffiliAmaze Plugin Information
Welcome to the AffiliAmaze plugin! Below is a guide on how to use this plugin effectively.
How to Use:
Create Affiliate Products: Go to 'Affiliate Products' and add new products. Enter product details such as image URL, title, Amazon URL, price, etc.
Insert Affiliate Frames: Use the [AffiliAmaze] shortcode in your posts or pages to insert affiliate links. Provide the 'product_id' attribute with the ID of the affiliate product you created. Shortcode: [AffiliAmaze product_id="YOUR_PRODUCT_ID"]
Customize Settings: Visit the 'Settings' page to customize plugin settings such as button text, Amazon affiliate ID, display options, etc.
Plugin Settings:
To customize plugin settings:
Go to 'Settings' in the WordPress admin menu.
Select 'AffiliAmaze Settings'.
Adjust settings according to your preferences.
Save changes.
Need Help?
If you have any questions or need assistance, feel free to contact our support team at smarthomestarter01@gmail.com.
' . esc_html__('Import Affiliate Products', 'affiliamaze-amazon-affiliate-linker') . '';
echo '';
if (isset($_FILES['import_file'])) {
try {
// Validate the nonce
if (!isset($_POST['import_affiliate_products_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['import_affiliate_products_nonce'])), 'import_affiliate_products_action')) {
throw new Exception(esc_html__('Invalid nonce.', 'affiliamaze-amazon-affiliate-linker'));
}
// Check for upload errors
if ($_FILES['import_file']['error'] !== UPLOAD_ERR_OK) {
$file_error = sanitize_text_field($_FILES['import_file']['error']);
throw new Exception(esc_html__('Error uploading file.', 'affiliamaze-amazon-affiliate-linker') . ' ' . esc_html($file_error));
}
// File path sanitize
$file = $_FILES['import_file']['tmp_name']; // Verwenden Sie direkt den temporären Dateipfad
$file = realpath($file); // Stellen Sie sicher, dass der Pfad korrekt ist
if (!$file || !file_exists($file)) {
throw new Exception(esc_html__('File does not exist or path is incorrect.', 'affiliamaze-amazon-affiliate-linker'));
}
$imported_data = json_decode(file_get_contents($file), true, 512, JSON_THROW_ON_ERROR);
foreach ($imported_data as $product) {
if (!isset($product['Product Title']) || !isset($product['Image URL']) || !isset($product['Amazon URL'])) {
continue;
}
$new_post = array(
'post_title' => sanitize_text_field($product['Product Title']),
'post_type' => 'affiliate_product',
'post_status' => 'publish',
);
$post_id = wp_insert_post($new_post);
if ($post_id) {
update_post_meta($post_id, 'affiliate_image_url', esc_url_raw($product['Image URL']));
update_post_meta($post_id, 'affiliate_product_title', sanitize_text_field($product['Product Title']));
update_post_meta($post_id, 'affiliate_amazon_url', esc_url_raw($product['Amazon URL']));
update_post_meta($post_id, 'affiliate_price', isset($product['Price']) ? sanitize_text_field($product['Price']) : '');
update_post_meta($post_id, 'affiliate_amazon_market', isset($product['Amazon Market']) ? sanitize_text_field($product['Amazon Market']) : 'de');
update_post_meta($post_id, 'affiliate_show_prime_logo', isset($product['Show Prime Logo']) ? sanitize_text_field($product['Show Prime Logo']) : 'no');
update_post_meta($post_id, 'affiliate_show_bestseller_label', isset($product['Show Bestseller Label']) ? sanitize_text_field($product['Show Bestseller Label']) : 'no');
// Debug output
echo '
';
}
}
}
// Ensure to add this function in your plugin
add_action('admin_menu', 'affiliate_linker_create_affiliate_product_cpt_and_menu');
// Nonce security for export function
function affiliate_linker_render_export_json_page() {
echo '
';
echo '
' . esc_html__('Export Affiliate Products as JSON', 'affiliamaze-amazon-affiliate-linker') . '
';
// Check whether the export button was pressed
if (isset($_POST['export_json'])) {
// Security check
if (!isset($_POST['export_nonce_field']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['export_nonce_field'])), 'export_nonce')) {
return;
}
// Call the export function and get the path to the file
$file_url = affiliate_linker_export_affiliate_products_to_json();
// Redirect user to download the file
echo "";
}
// Export button form
echo '';
echo '
';
}
function affiliate_linker_export_affiliate_products_to_json() {
if (!current_user_can('manage_options')) {
wp_die(esc_html__('Insufficient permissions', 'affiliamaze-amazon-affiliate-linker'));
}
// Collect affiliate product data
$args = array(
'post_type' => 'affiliate_product',
'posts_per_page' => -1,
);
$affiliate_products = get_posts($args);
$data_to_export = array();
foreach ($affiliate_products as $product) {
$data_to_export[] = array(
'Product ID' => $product->ID,
'Product Title' => get_post_meta($product->ID, 'affiliate_product_title', true),
'Image URL' => get_post_meta($product->ID, 'affiliate_image_url', true),
'Amazon URL' => get_post_meta($product->ID, 'affiliate_amazon_url', true),
'Price' => get_post_meta($product->ID, 'affiliate_price', true),
'Amazon Market' => get_post_meta($product->ID, 'affiliate_amazon_market', true),
'Show Prime Logo' => get_post_meta($product->ID, 'affiliate_show_prime_logo', true) === 'yes' ? true : false,
'Show Bestseller Label' => get_post_meta($product->ID, 'affiliate_show_bestseller_label', true) === 'yes' ? true : false,
// Add more data here
);
}
// Convert data to JSON
$json_data = json_encode($data_to_export, JSON_PRETTY_PRINT);
// Save file in uploads directory
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['basedir'] . '/affiliate_products_export.json';
file_put_contents($file_path, $json_data);
// Return download link
return $upload_dir['baseurl'] . '/affiliate_products_export.json';
}
// Trigger export when a new product is saved
function affiliate_linker_trigger_export_on_save_post($post_id) {
// Check if the saved post is an affiliate product
if (get_post_type($post_id) === 'affiliate_product') {
// Call the export function
affiliate_linker_export_affiliate_products_to_json();
}
}
add_action('save_post', 'affiliate_linker_trigger_export_on_save_post');
// Callback function settings page
function affiliate_linker_affiliate_link_settings_submenu() {
// Settings page content
echo '