Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a company taxonomy to ads managed by the Advanced Ads plugin.
<?php
/**
* Advanced Ads – Company Category
*
* @wordpress-plugin
* Plugin Name: Advanced Ads – Company Category
* Plugin URI: https://wpadvancedads.com
* Description: Add a Company category to ads so that you can group them.
* Version: 0.1
* Author: Thomas Maier
* Author URI: https://wpadvancedads.com
* Domain Path: /languages
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*
* Features:
*
* - adds a "Company" categorization to ads
* - adds a category-like meta box to the ad edit screen to choose the company
* - lists the company in a dedicated column in the ad overview list
*
* Limitations:
*
* - the ad overview list can’t be filtered by the company => needs a fix
*
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Register the Company taxonomy
*/
function advanced_ads_company_category_register_taxonomy() {
if ( ! taxonomy_exists( 'advanced_ads_company_taxonomy' ) ) {
$labels = array(
'name' => 'Companies',
'singular_name' => 'Company',
'search_items' => 'Search Company',
'all_items' => 'All Companies',
'parent_item' => 'Parent Companies',
'parent_item_colon' => 'Parent Companies:',
'edit_item' => 'Edit Company',
'update_item' => 'Update Company',
'add_new_item' => 'Add New Company',
'new_item_name' => 'New Company Name',
'menu_name' => 'Companies',
'not_found' => 'No Company found',
);
$args = array(
'public' => false,
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'show_admin_column' => true,
'query_var' => false,
'rewrite' => false,
);
register_taxonomy( 'advanced_ads_company_taxonomy', array( 'advanced_ads' ), $args );
}
}
add_action( 'init', 'advanced_ads_company_category_register_taxonomy' );
/**
* Whitelist the Company taxonomy box on the ad edit page
*/
function advanced_ads_company_category_whitelist_metabox( $metaboxes ) {
$metaboxes[] = 'advanced_ads_company_taxonomydiv';
return $metaboxes;
}
add_filter( 'advanced-ads-ad-edit-allowed-metaboxes', 'advanced_ads_company_category_whitelist_metabox' );
/**
* Whitelist Company column in ad overview list
*/
function advanced_ads_company_category_whitelist_column( $columns ) {
$columns[] = 'taxonomy-advanced_ads_company_taxonomy';
return $columns;
}
add_filter( 'advanced-ads-ad-list-allowed-columns', 'advanced_ads_company_category_whitelist_column' );
/**
* Create a company list that will be displayed in the filter list
*/
function advanced_ads_company_category_collect_filters( $all_filters, $post, $ad_option ) {
$anc = advanced_ads_company_storage::get_ads_and_company();
// no companies.
if ( false === $anc ) {
return $all_filters;
}
foreach ( $anc['ads_by_company'] as $comp_id => $ad_ids ) {
if ( in_array( $post->ID, $ad_ids, true ) ) {
if ( ! isset( $all_filters['all_companies'] ) ) {
$all_filters['all_companies'] = array();
}
if ( ! isset( $all_filters['all_companies'][ $comp_id ] ) ) {
$all_filters['all_companies'][ $comp_id ] = $anc['all_companies'][ $comp_id ]['slug'];
}
}
}
return $all_filters;
}
add_filter( 'advanced-ads-ad-list-column-filter', 'advanced_ads_company_category_collect_filters', 10, 3 );
/**
* Add a select box for companies
*/
function advanced_ads_company_add_markup( $all_filters ) {
if ( ! empty( $all_filters['all_companies'] ) ) {
$anc = advanced_ads_company_storage::get_ads_and_company();
echo '<select id="advads-filter-company" name="adcompany">';
echo '<option value="">- All companies -</option>';
foreach ( $all_filters['all_companies'] as $id => $slug ) {
echo '<option value="' . esc_attr( $id ) . '">' . esc_html( $anc['all_companies'][ $id ]['name'] ) . '</option>';
}
echo '</select>';
}
}
add_action( 'advanced-ads-ad-list-filter-markup', 'advanced_ads_company_add_markup' );
/**
* Add company filter on ad overview page
*
* @param array $posts list of posts.
* @param array $all_ads_options additional options.
*
* @return array filtered posts
*/
function advanced_ads_company_filter( $posts, $all_ads_options ) {
$request = wp_unslash( $_REQUEST );
if ( isset( $request['adcompany'] ) && ! empty( $request['adcompany'] ) ) {
$anc = advanced_ads_company_storage::get_ads_and_company();
if ( false === $anc ) {
return $posts;
}
$new_posts = array();
foreach ( $posts as $post ) {
if ( in_array( $post->ID, $anc['ads_by_company'][ absint( $request['adcompany'] ) ], true ) ) {
$new_posts[] = $post;
}
}
$posts = $new_posts;
}
return $posts;
}
add_filter( 'advanced-ads-ad-list-filter', 'advanced_ads_company_filter', 10, 2 );
if ( ! class_exists( 'advanced_ads_company_storage' ) ) {
class advanced_ads_company_storage {
private static $ads_company = null;
public static function get_ads_and_company() {
if ( null === self::$ads_company ) {
global $wpdb;
$q1 = "SELECT `term_id`, `name`, `slug` FROM `{$wpdb->prefix}terms` WHERE `term_id` IN " .
"( SELECT `term_id` FROM `{$wpdb->prefix}term_taxonomy` WHERE `taxonomy` = 'advanced_ads_company_taxonomy' )";
/**
* All companies
*/
$_all_companies = $wpdb->get_results( $q1, 'ARRAY_A' );
if ( empty( $_all_companies ) ) {
self::$ads_company = false;
return false;
}
$all_companies = array();
foreach ( $_all_companies as $c ) {
$all_companies[ absint( $c['term_id'] ) ] = array(
'name' => $c['name'],
'slug' => $c['slug'],
);
}
$q2 = "SELECT `object_id`, `term_taxonomy_id` FROM `{$wpdb->prefix}term_relationships` WHERE `term_taxonomy_id` IN " .
"( SELECT `term_id` FROM `{$wpdb->prefix}term_taxonomy` WHERE `taxonomy` = 'advanced_ads_company_taxonomy' )";
$ads = $wpdb->get_results( $q2, 'ARRAY_A' );
/**
* Group the ad id-s by company.
*/
$anc = array();
foreach ( $ads as $ad ) {
if ( ! isset( $anc[ absint( $ad['term_taxonomy_id'] ) ] ) ) {
$anc[ absint( $ad['term_taxonomy_id'] ) ] = array();
}
$anc[ absint( $ad['term_taxonomy_id'] ) ][] = absint( $ad['object_id'] );
}
$results = array(
'ads_by_company' => $anc,
'all_companies' => $all_companies,
);
self::$ads_company = $results;
return $results;
} else {
return self::$ads_company;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment