Skip to content

Instantly share code, notes, and snippets.

@SubCon
Created June 15, 2022 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SubCon/e4ab79fdb4d40100dfdbe811b8e3d347 to your computer and use it in GitHub Desktop.
Save SubCon/e4ab79fdb4d40100dfdbe811b8e3d347 to your computer and use it in GitHub Desktop.
Field editor hooks to add functionality
<?php
/**
* Jobify
*
* Do not modify this file. Place all modifications in a child theme.
*
* @package Jobify
* @category Theme
* @since 1.0.0
*/
/***
* Add new Tab After Packages
* tab: Pack Conditions
*
*/
add_filter('field_editor_default_modal_fields', 'jmfe_add_modal_tab', 999999, 1);
function jmfe_add_modal_tab($modal_fields){
$packages = $wc_job_packages = array();
$packages = WP_Job_Manager_Field_Editor_Package_WC::get_packages( FALSE, 'job' );
if( ! empty( $packages ) ) {
$modal_fields['tabs']['pack_conditions'] = array(
'label' => __( 'Pack Conditions', 'wp-job-manager-field-editor' ),
'help' => array(
'icon' => 'question',
'url' => 'https://plugins.smyl.es/docs-kb/showhide-specific-fields-based-on-selected-package/'
),
'fields' => array(
'pack_cond_require' => array(
'label' => __( 'Require', 'wp-job-manager-field-editor' ),
'caption' => __( 'Require specific packages to display this field', 'wp-job-manager-field-editor' ),
'type' => 'checkbox',
'default' => '1||Enable',
),
'pack_cond_show' => array(
'label' => __( 'Packages', 'wp-job-manager-field-editor' ),
'caption' => __( 'Select packages you want this field to show for. Require checkbox above must be enabled for this to work.', 'wp-job-manager-field-editor' ),
'type' => 'checkbox',
'default' => $packages,
)
)
);
}
return $modal_fields;
}
add_filter('field_editor_update_post_meta_skip_update', 'jmfe_update_modal_skip_fields_meta', 999999, 1);
function jmfe_update_modal_skip_fields_meta($skip_update){
$skip_update[] = 'pack_cond_show';
return $skip_update;
}
add_filter('field_editor_update_post_meta_checkboxes', 'jmfe_update_modal_checkboxes_fields_meta', 999999, 1);
function jmfe_update_modal_checkboxes_fields_meta($checkboxes){
$checkboxes[] = 'pack_cond_require';
return $checkboxes;
}
add_action('field_editor_update_field_post_meta_end', 'jmfe_update_modal_array_fields', 999999, 5);
function jmfe_update_modal_array_fields($post_id, $meta_key, $field_type, $action, $old_meta){
// Handle Packages Show array
$pack_cond_show = isset( $_POST[ 'pack_cond_show' ] ) ? $_POST[ 'pack_cond_show' ] : array();
// If no packages are select remove post meta
if ( $action === "edit" && empty( $pack_cond_show ) ) delete_post_meta( $post_id, 'pack_cond_show' );
if ( ! empty( $pack_cond_show ) ){
// Check if packages_show was submitted as array of IDs
if( isset($pack_cond_show[0]) ){
foreach( $pack_cond_show[0] as $product_id ) {
$enabled_packages[] = $product_id;
}
} else {
// Or if only one package exists, it will show up as a string in POST
$enabled_packages[] = $pack_cond_show;
}
update_post_meta( $post_id, 'pack_cond_show', $enabled_packages );
}
}
/*** -------------- /end adding new Pack Conditions tab -------------- ***/
add_filter('field_editor_auto_output_do_auto_output', 'jmfe_field_do_auto_output', 999999, 6);
function jmfe_field_do_auto_output( $do_auto_output_flag, $action, $meta_key, $config, $fields, $is_wpcm_field)
{
if($meta_key == 'job_phone'){
//package=57
/**
* Get job ID (86)
* Get Ordered package by job_id
* compare package ID's
*
*/
$job_id = get_the_ID();
$job_package_id = get_post_meta($job_id, '_package_id', true);
if(!empty($config['packages_show'])){
if(!in_array($job_package_id, $config['packages_show'])){
$do_auto_output_flag = false;
}
}
}
return $do_auto_output_flag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment