Inventory_Presser_Plugin::add_hooks()

This is the driver function of the entire plugin. Includes dependencies and adds all hooks.

On This Page


Return Return

(void)


Top ↑

Source Source

File: inventory-presser.php

		public function add_hooks() {
			// Only do these things once.
			if ( has_filter( 'invp_prefix_meta_key', array( 'INVP', 'translate_custom_field_names' ) ) ) {
				return;
			}

			// include all this plugin's classes that live in external files.
			$this->include_dependencies();

			// Prefix and unprefix meta keys with "inventory_presser_".
			add_filter( 'invp_prefix_meta_key', array( 'INVP', 'translate_custom_field_names' ) );
			add_filter( 'invp_unprefix_meta_key', array( 'INVP', 'untranslate_custom_field_names' ) );

			// Provide a path to .mo files in /languages.
			add_action( 'init', array( $this, 'load_textdomain' ) );

			/**
			 * Create our post type and taxonomies
			 */

			// create a custom post type for the vehicles.
			add_action( 'init', array( $this, 'create_post_type' ) );

			// register all postmeta fields the CPT uses.
			add_action( 'init', array( $this, 'register_meta_fields' ), 20 );

			// Filter the attachment post type to make sure `parent` is exposed in the REST API.
			add_filter( 'register_post_type_args', array( $this, 'edit_attachment_post_type' ), 10, 2 );

			/**
			 * Make sure our own classes exist before creating instances in case
			 * a page load happens during a plugin update when some of these
			 * files might actually not exist.
			 */

			// Create custom taxonomies.
			if ( class_exists( 'Inventory_Presser_Taxonomies' ) ) {
				$taxonomies = new Inventory_Presser_Taxonomies();
				$taxonomies->add_hooks();
			}

			// Modify edit-tags.php for our location taxonomy to manage term meta.
			if ( class_exists( 'Inventory_Presser_Admin_Location_Meta' ) ) {
				$location_meta = new Inventory_Presser_Admin_Location_Meta();
				$location_meta->add_hooks();
			}

			/**
			 * Some custom rewrite rules are created and destroyed
			 */

			// Add custom rewrite rules.
			add_action( 'generate_rewrite_rules', array( $this, 'add_pretty_search_urls' ) );

			/**
			 * Activation and deactivation hooks ensure that the rewrite rules are
			 * flushed to add and remove our custom rewrite rules
			 */

			// Flush rewrite rules when the plugin is activated.
			register_activation_hook( INVP_PLUGIN_FILE_PATH, array( __CLASS__, 'flush_rewrite' ) );

			// Set default settings values.
			register_activation_hook( INVP_PLUGIN_FILE_PATH, array( __CLASS__, 'set_default_settings' ) );

			// Delete an option during deactivation.
			register_deactivation_hook( INVP_PLUGIN_FILE_PATH, array( __CLASS__, 'delete_rewrite_rules_option' ) );

			// Register some widgets included with this plugin.
			add_action( 'widgets_init', array( $this, 'register_widgets' ) );

			/**
			 * Deliver our promise to order posts, change the ORDER BY clause of
			 * the query that's fetching post objects.
			 */
			if ( ! is_admin() ) {
				add_action( 'pre_get_posts', array( $this, 'add_orderby_to_query' ) );
				add_action( 'pre_get_posts', array( $this, 'modify_query_for_max_price' ), 99, 1 );
			}

			// Register scripts and styles.
			add_action( 'wp_enqueue_scripts', array( __CLASS__, 'include_scripts_and_styles' ) );
			add_action( 'admin_enqueue_scripts', array( __CLASS__, 'include_scripts_and_styles' ) );
			add_action( 'enqueue_block_assets', array( __CLASS__, 'include_scripts_and_styles' ) );

			// Allow custom fields to be searched.
			if ( class_exists( 'Add_Custom_Fields_To_Search' ) ) {
				$add_custom_fields_to_search = new Add_Custom_Fields_To_Search();
				$add_custom_fields_to_search->add_hooks();
			}

			// Redirect URLs by VINs to proper vehicle permalinks.
			if ( class_exists( 'Vehicle_URLs_By_VIN' ) ) {
				$allow_urls_by_vin = new Vehicle_URLs_By_VIN();
				$allow_urls_by_vin->add_hooks();
			}

			// Add buttons near vehicles for Carfax reports or NextGear inspections.
			if ( class_exists( 'Inventory_Presser_Badges' ) ) {
				$badges = new Inventory_Presser_Badges();
				$badges->add_hooks();
			}

			// Redirect 404 vehicles to make archives.
			if ( class_exists( 'Redirect_404_Vehicles' ) ) {
				$redirect_404_vehicles = new Redirect_404_Vehicles();
				$redirect_404_vehicles->add_hooks();
			}

			// Modify the URL of an "Email a Friend" menu item on the "Vehicle Details Buttons" menu.
			if ( class_exists( 'Inventory_Presser_Email_A_Friend' ) ) {
				$email_a_friend = new Inventory_Presser_Email_A_Friend();
				$email_a_friend->add_hooks();
			}

			// Make it possible for a menu item to print the page.
			if ( class_exists( 'Inventory_Presser_Menu_Item_Print' ) ) {
				$print_button = new Inventory_Presser_Menu_Item_Print();
				$print_button->add_hooks();
			}

			/**
			 * When vehicle posts are inserted, make sure they create a relationship
			 * with the "For Sale" term in the Availabilities taxonomy. Some queries
			 * that honor the "Include Sold Vehicles" setting in this plugin will
			 * exclude them without a relationship to a term in that taxonomy.
			 */
			if ( class_exists( 'INVP' ) ) {
				// Everything For Sale because by default we hide other Availabilities like Sold.
				add_action( 'save_post_' . INVP::POST_TYPE, array( $this, 'mark_vehicles_for_sale_during_insertion' ), 10, 3 );
				// Photos are sometimes attached to posts in simultaneous requests.
				add_action( 'save_post_' . INVP::POST_TYPE, array( 'Inventory_Presser_Photo_Numberer', 'renumber_photos' ), 10, 1 );
				// Save custom post meta and term relationships when posts are saved.
				add_action( 'save_post_' . INVP::POST_TYPE, array( $this, 'save_vehicle_post_meta' ), 10, 3 );
				add_action( 'save_post_' . INVP::POST_TYPE, array( $this, 'save_vehicle_taxonomy_terms' ), 10, 2 );
			}

			// Maybe skip the trash bin and permanently delete vehicles & photos.
			add_action( 'trashed_post', array( $this, 'maybe_force_delete' ) );

			// When vehicles are deleted, delete their attachments, too.
			add_action( 'before_delete_post', array( 'INVP', 'delete_attachments' ), 10, 1 );

			// Change messages in the dashboard when updating vehicles.
			add_filter( 'post_updated_messages', array( $this, 'change_post_updated_messages' ) );

			// Change links to our taxonomy terms to insert /inventory/.
			add_filter( 'pre_term_link', array( $this, 'change_term_links' ), 10, 2 );

			// Allow users to set the Inventory listing page as the home page.
			if ( class_exists( 'Inventory_Presser_Allow_Inventory_As_Home_Page' ) ) {
				$page = new Inventory_Presser_Allow_Inventory_As_Home_Page();
				$page->add_hooks();
			}

			// Add all our shortcodes.
			if ( class_exists( 'Inventory_Presser_Shortcode_Grid' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Grid();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Iframe' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Iframe();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Slider' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Slider();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Single_Vehicle' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Single_Vehicle();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Archive' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Archive();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Archive_Vehicle' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Archive_Vehicle();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Attribute_Table' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Attribute_Table();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Hours_Today' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Hours_Today();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Photo_Slider' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Photo_Slider();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Vin' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Vin();
				$shortcodes->add_hooks();
			}
			if ( class_exists( 'Inventory_Presser_Shortcode_Sort_By' ) ) {
				$shortcodes = new Inventory_Presser_Shortcode_Sort_By();
				$shortcodes->add_hooks();
			}

			/**
			 * When the active theme isn't prepared to display vehicles, insert
			 * our archive and single vehicle shortcodes.
			 */
			if ( class_exists( 'Inventory_Presser_Template_Provider' ) ) {
				$template_provider = new Inventory_Presser_Template_Provider();
				$template_provider->add_hooks();
			}

			// Add blocks.
			if ( class_exists( 'Inventory_Presser_Blocks' ) ) {
				$blocks = new Inventory_Presser_Blocks();
				$blocks->add_hooks();
			}

			/**
			 * Add photo number meta values to vehicle photos uploaded in the
			 * dashboard
			 */
			if ( class_exists( 'Inventory_Presser_Photo_Numberer' ) ) {
				$photo_numberer = new Inventory_Presser_Photo_Numberer();
				$photo_numberer->add_hooks();
			}

			// Allow additional vehicle archives to be created.
			if ( class_exists( 'Inventory_Presser_Additional_Listings_Pages' ) ) {
				$additional_archives = new Inventory_Presser_Additional_Listings_Pages();
				$additional_archives->add_hooks();
			}

			// Add menus to the admin bar.
			if ( class_exists( 'Inventory_Presser_Admin_Bar' ) ) {
				$bar = new Inventory_Presser_Admin_Bar();
				$bar->add_hooks();
			}

			if ( is_admin() ) {
				// Initialize our Settings page in the Dashboard.
				if ( class_exists( 'Inventory_Presser_Admin_Options' ) ) {
					$options = new Inventory_Presser_Admin_Options();
					$options->add_hooks();
				}

				// Add columns to the vehicle posts list.
				if ( class_exists( 'Inventory_Presser_Admin_Posts_List' ) ) {
					$posts_list = new Inventory_Presser_Admin_Posts_List();
					$posts_list->add_hooks();
				}

				// Add a sidebar to the editor when editing vehicles.
				if ( class_exists( 'Inventory_Presser_Admin_Editor_Sidebar' ) ) {
					$sidebar = new Inventory_Presser_Admin_Editor_Sidebar();
					$sidebar->add_hooks();
				}

				// Allow more than 30 meta keys in the Custom Fields editor panel.
				add_filter( 'postmeta_form_limit', array( $this, 'custom_fields_key_limit' ) );
			}

			if ( class_exists( 'Inventory_Presser_Taxonomy_Overlapper' ) ) {
				$overlapper = new Inventory_Presser_Taxonomy_Overlapper();
				$overlapper->add_hooks();
			}

			if ( class_exists( 'Inventory_Presser_Schema_Org_Generator' ) ) {
				$schema_generator = new Inventory_Presser_Schema_Org_Generator();
				$schema_generator->add_hooks();
			}

			// Adds the "View Details" button to each vehicle in archive loops.
			add_action( 'invp_archive_buttons', array( $this, 'add_view_details_button' ) );

			// Embeds a contact form on vehicle singles if one is chosen at Vehicles → Options.
			add_action( 'invp_single_sections', array( $this, 'single_sections_add_form' ) );

			add_action( 'plugins_loaded', array( $this, 'loaded' ) );

			if ( class_exists( 'Inventory_Presser_REST' ) ) {
				$rest = new Inventory_Presser_REST();
				$rest->add_hooks();
			}

			if ( class_exists( 'Inventory_Presser_Admin_Photo_Arranger' ) ) {
				$photo_arranger = new Inventory_Presser_Admin_Photo_Arranger();
				$photo_arranger->add_hooks();
			}

			// Change archive page titles.
			add_filter( 'document_title_parts', array( $this, 'change_archive_title_tags' ) );

			// Add a link to the Settings page on the plugin management page.
			add_filter( 'plugin_action_links_' . INVP_PLUGIN_BASE, array( $this, 'insert_settings_link' ), 2, 2 );

			// Add tests to the Site Health Status page.
			if ( class_exists( 'Inventory_Presser_Site_Health' ) ) {
				$health = new Inventory_Presser_Site_Health();
				$health->add_hooks();
			}

			add_action( 'plugins_loaded', array( $this, 'load_integrations' ) );
		}