*/ namespace AffiLinks; use WP_Query; use AffiLinks\Track; defined( 'ABSPATH' ) || exit; /** * Post class. */ class Post { /** * The single instance of the class. * * @var Post */ protected static $instance = null; /** * Retrieve main Post instance. * * Ensure only one instance is loaded or can be loaded. * * @return Post */ public static function get() { if ( is_null( self::$instance ) && ! ( self::$instance instanceof Post ) ) { self::$instance = new Post(); } return self::$instance; } /** * Constructor. */ public function __construct() { add_action( 'init', [ $this, 'register_post_type' ] ); add_action( 'admin_menu', [ $this, 'admin_menu' ] ); add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue' ] ); add_action( 'save_post_aff_brand', [ $this, 'save_brand_fields' ] ); add_action( 'save_post_aff_link', [ $this, 'save_link_fields' ] ); add_action( 'save_post_aff_asset', [ $this, 'save_asset_fields' ] ); add_filter( 'manage_aff_link_posts_columns', [ $this, 'add_link_columns' ] ); add_action( 'manage_aff_link_posts_custom_column', [ $this, 'add_link_columns_markup' ], 10, 2 ); add_filter( 'manage_aff_asset_posts_columns', [ $this, 'add_asset_columns' ] ); add_action( 'manage_aff_asset_posts_custom_column', [ $this, 'add_asset_columns_markup' ], 10, 2 ); } /** * Add link columns markup. * * @param string $column Column name. * @param int $post_id Post ID. */ public function add_asset_columns_markup( $column, $post_id ) { if ( 'link' === $column ) { $link = self::get_link( $post_id ); echo ''; } if ( 'shortcode' === $column ) { $shortcode = "[aff_asset id='{$post_id}']"; echo ''; } } /** * Add asset columns. * * @param array $columns Columns. */ public function add_asset_columns( $columns ) { unset( $columns['date'] ); $columns['link'] = esc_html__( 'Link', 'affilinks' ); $columns['shortcode'] = esc_html__( 'Shortcode', 'affilinks' ); $columns['date'] = esc_html__( 'Date', 'affilinks' ); return $columns; } /** * Add link columns. * * @param array $columns Columns. */ public function add_link_columns( $columns ) { unset( $columns['date'] ); $columns['link'] = esc_html__( 'Link', 'affilinks' ); $columns['shortcode'] = esc_html__( 'Shortcode', 'affilinks' ); $columns['date'] = esc_html__( 'Date', 'affilinks' ); return $columns; } /** * Add link columns markup. * * @param string $column Column name. * @param int $post_id Post ID. */ public function add_link_columns_markup( $column, $post_id ) { if ( 'shortcode' === $column ) { $shortcode = "[aff_link id='{$post_id}']Content[/aff_link]"; echo ''; } if ( 'link' === $column ) { $link = self::get_link( $post_id ); echo ''; } } /** * Register post types. */ public function register_post_type() { $labels = [ 'name' => esc_html_x( 'Brands', 'post type general name', 'affilinks' ), 'singular_name' => esc_html_x( 'Brand', 'post type singular name', 'affilinks' ), 'add_new' => esc_html_x( '→ Add Brand', 'affiliate brand', 'affilinks' ), 'add_new_item' => esc_html__( 'Add New Affiliate Brand', 'affilinks' ), 'edit_item' => esc_html__( 'Edit Affiliate Brand', 'affilinks' ), 'new_item' => esc_html__( 'New Affiliate Brand', 'affilinks' ), 'all_items' => esc_html__( 'All Brands', 'affilinks' ), 'view_item' => esc_html__( 'View Affiliate Brand', 'affilinks' ), 'search_items' => esc_html__( 'Search Affiliate Brands', 'affilinks' ), 'not_found' => esc_html__( 'No Affiliate Brands found', 'affilinks' ), 'not_found_in_trash' => esc_html__( 'No Affiliate Brands found in the Trash', 'affilinks' ), 'menu_name' => esc_html__( 'AffiLinks', 'affilinks' ), ]; $args = [ 'labels' => $labels, 'menu_icon' => 'dashicons-admin-links', 'supports' => [ 'title' ], 'has_archive' => false, 'public' => false, 'publicly_queryable' => false, 'exclude_from_search' => true, 'show_ui' => true, ]; register_post_type( 'aff_brand', $args ); $labels = [ 'name' => esc_html_x( 'Affiliate Links', 'post type general name', 'affilinks' ), 'singular_name' => esc_html_x( 'Affiliate Link', 'post type singular name', 'affilinks' ), 'add_new' => esc_html_x( 'Add New Link', 'affiliate link', 'affilinks' ), 'add_new_item' => esc_html__( 'Add New Affiliate Link', 'affilinks' ), 'edit_item' => esc_html__( 'Edit Affiliate Link', 'affilinks' ), 'new_item' => esc_html__( 'New Affiliate Link', 'affilinks' ), 'all_items' => esc_html__( 'All Links', 'affilinks' ), 'view_item' => esc_html__( 'View Affiliate Link', 'affilinks' ), 'search_items' => esc_html__( 'Search Affiliate Links', 'affilinks' ), 'not_found' => esc_html__( 'No affiliate links found', 'affilinks' ), 'not_found_in_trash' => esc_html__( 'No affiliate links found in the Trash', 'affilinks' ), ]; $args = [ 'labels' => $labels, 'menu_icon' => 'dashicons-admin-links', 'supports' => [ 'title' ], 'has_archive' => true, 'public' => false, 'publicly_queryable' => false, 'exclude_from_search' => true, 'show_ui' => true, ]; register_post_type( 'aff_link', $args ); $labels = [ 'name' => _x( 'Affiliate Assets', 'post type general name', 'affilinks' ), 'singular_name' => _x( 'Affiliate Asset', 'post type singular name', 'affilinks' ), 'add_new' => _x( 'Add New Asset', 'affiliate Asset', 'affilinks' ), 'add_new_item' => __( 'Add New Affiliate Asset', 'affilinks' ), 'edit_item' => __( 'Edit Affiliate Asset', 'affilinks' ), 'new_item' => __( 'New Affiliate Asset', 'affilinks' ), 'all_items' => __( 'All Affiliate Assets', 'affilinks' ), 'view_item' => __( 'View Affiliate Asset', 'affilinks' ), 'search_items' => __( 'Search Affiliate Assets', 'affilinks' ), 'not_found' => __( 'No affiliate Assets found', 'affilinks' ), 'not_found_in_trash' => __( 'No affiliate Assets found in the Trash', 'affilinks' ), 'menu_name' => esc_html__( 'Affiliate Assets', 'affilinks' ), ]; $args = [ 'labels' => $labels, 'menu_position' => 5, 'supports' => [ 'title', 'editor' ], 'has_archive' => true, 'public' => false, 'publicly_queryable' => false, 'exclude_from_search' => true, 'show_ui' => true, ]; register_post_type( 'aff_asset', $args ); if ( 'yes' === get_option( 'affilinks_flush_rewrite_rules', 'no' ) ) { flush_rewrite_rules(); update_option( 'affilinks_flush_rewrite_rules', 'no' ); } } /** * Admin menu. */ public function admin_menu() { add_submenu_page( 'edit.php?post_type=aff_brand', esc_html__( 'Links', 'affilinks' ), esc_html__( 'Links', 'affilinks' ), 'manage_options', 'edit.php?post_type=aff_link' ); add_submenu_page( 'edit.php?post_type=aff_brand', esc_html__( '→ Add Link', 'affilinks' ), esc_html__( '→ Add Link', 'affilinks' ), 'manage_options', 'post-new.php?post_type=aff_link' ); add_submenu_page( 'edit.php?post_type=aff_brand', esc_html__( 'Assets', 'affilinks' ), esc_html__( 'Assets', 'affilinks' ), 'manage_options', 'edit.php?post_type=aff_asset' ); add_submenu_page( 'edit.php?post_type=aff_brand', esc_html__( '→ Add Asset', 'affilinks' ), esc_html__( '→ Add Asset', 'affilinks' ), 'manage_options', 'post-new.php?post_type=aff_asset' ); remove_menu_page( 'edit.php?post_type=aff_link' ); remove_menu_page( 'edit.php?post_type=aff_asset' ); } /** * Admin enqueue. * * @param string $hook Hook. */ public function admin_enqueue( $hook = '' ) { $post_type = get_current_screen()->post_type; if ( ! in_array( $post_type, [ 'aff_brand', 'aff_link', 'aff_asset' ], true ) ) { return; } wp_enqueue_style( 'affilinks-metabox', AFFILINKS_URI . 'assets/admin/css/metabox.css', [], AFFILINKS_VERSION, 'all' ); wp_enqueue_script( 'affilinks-shortlink', AFFILINKS_URI . 'assets/admin/js/shortlink.js', [ 'jquery' ], AFFILINKS_VERSION, true ); wp_localize_script( 'affilinks-shortlink', 'AffiLinksShortLinkVars', [ 'site_url' => site_url(), ] ); } /** * Add meta boxes */ public function add_meta_boxes() { add_meta_box( 'aff_brand_fields', esc_html__( 'Brand Details', 'affilinks' ), [ $this, 'render_brand_fields' ], 'aff_brand', 'normal', 'default' ); add_meta_box( 'aff_link_fields', esc_html__( 'Link Details', 'affilinks' ), [ $this, 'render_link_fields' ], 'aff_link', 'normal', 'default' ); add_meta_box( 'aff_asset_fields', esc_html__( 'Link Details', 'affilinks' ), [ $this, 'render_asset_fields' ], 'aff_asset', 'normal' ); } /** * Render Brand Fields * * @param WP_Post $post Current post object. */ public function render_brand_fields( $post ) { $brand_url = get_post_meta( $post->ID, 'aff_brand_url', true ); $affiliate_page_url = get_post_meta( $post->ID, 'aff_affiliate_page_url', true ); ?>