m_key; $is_draft = false; } else { return $total; } $edit_url = self::get_edit_url( $type, $object_id ); $total[] = array( 'itemId' => $object_id, 'itemKey' => $object_key, 'name' => $name, 'type' => $type, 'descriptiveType' => $descriptive_type, 'typeLabel' => $type_label, 'editUrl' => $edit_url, 'parentOf' => self::get_parent_of_data( $current ), 'embeddedIn' => self::get_embedded_in_data( $current ), 'isDraft' => $is_draft, ); return $total; } /** * @param int $id * @return array|array */ private static function get_children_for_application( $id ) { $forms = FrmProApplication::get_forms_for_application( $id ); $posts = FrmProApplication::get_posts_for_application( $id ); self::maybe_sync_counts( $id, count( $forms ), $posts ); self::$helper = new FrmProApplicationRelationHelper( $forms, $posts ); return array_merge( $forms, $posts ); } /** * Maybe update form/view/page counts on application. * They may go out of sync if a form or post gets moved to trash. * * @param int $id Application id. * @param int $form_count * @param array $posts * @return void */ private static function maybe_sync_counts( $id, $form_count, $posts ) { if ( $form_count !== self::get_count( $id, 'form' ) ) { FrmProApplication::update_form_count( $id ); } $post_counts = array( 'frm_display' => 0, 'page' => 0, ); foreach ( $posts as $post ) { if ( isset( $post_counts[ $post->post_type ] ) ) { ++$post_counts[ $post->post_type ]; } } if ( $post_counts['frm_display'] !== self::get_count( $id, 'view' ) ) { FrmProApplication::update_view_count( $id ); } if ( $post_counts['page'] !== self::get_count( $id, 'page' ) ) { FrmProApplication::update_page_count( $id ); } } /** * @param int $id Application id. * @param string $type supports 'view', 'page', and 'form'. * @return int */ private static function get_count( $id, $type ) { $count = get_term_meta( $id, '_frm_' . $type . '_count', true ); return is_numeric( $count ) ? absint( $count ) : 0; } /** * @param WP_Post|stdClass $item * @return array */ private static function get_parent_of_data( $item ) { return self::$helper->get_parent_of_data( $item ); } /** * @param WP_Post|stdClass $item * @return array */ private static function get_embedded_in_data( $item ) { return self::$helper->get_embedded_in_data( $item ); } /** * Create empty application via AJAX action. * * @return void */ public static function create_application() { FrmProApplicationsHelper::custom_application_permission_check(); check_ajax_referer( 'frm_ajax', 'nonce' ); $name = FrmAppHelper::get_post_param( 'application_name', '', 'sanitize_text_field' ); $term = FrmProApplication::create( $name ); if ( ! is_array( $term ) ) { wp_send_json_error( __( 'Unable to create application', 'formidable-pro' ) ); die(); } $term_id = $term['term_id']; $redirect = FrmProApplicationsHelper::get_edit_url( $term_id ); $data = compact( 'term_id', 'redirect' ); wp_send_json_success( $data ); die(); } /** * Delete application via AJAX action. * * @return void */ public static function delete_application() { FrmProApplicationsHelper::custom_application_permission_check(); check_ajax_referer( 'frm_ajax', 'nonce' ); $term_id = FrmAppHelper::get_post_param( 'term_id', '', 'absint' ); if ( ! $term_id ) { die(); } $term = get_term( $term_id, 'frm_application' ); if ( ! ( $term instanceof WP_Term ) ) { wp_send_json_error( 'Application does not exist' ); die(); } wp_delete_term( $term_id, 'frm_application' ); $data = array(); wp_send_json_success( $data ); die(); } /** * Add item to application via AJAX action. * * @return void */ public static function add_to_application() { FrmProApplicationsHelper::custom_application_permission_check(); check_ajax_referer( 'frm_ajax', 'nonce' ); $term_id = FrmAppHelper::get_post_param( 'term_id', 0, 'absint' ); if ( ! $term_id ) { die(); } $type = FrmAppHelper::get_post_param( 'type', '', 'sanitize_text_field' ); if ( ! $type || ! in_array( $type, array( 'form', 'page', 'view' ), true ) ) { die(); } $new = FrmAppHelper::get_post_param( 'new', 0, 'absint' ); if ( 1 === $new ) { $redirect = self::get_new_item_redirect( $type, $term_id ); if ( '' === $redirect ) { wp_send_json_error( 'Page name may be missing, or type is incorrect' ); } } else { $item_id = FrmAppHelper::get_post_param( 'item_id', 0, 'absint' ); if ( ! $item_id ) { die(); } self::add_existing_item_to_application( $type, $term_id, $item_id ); $data = array(); wp_send_json_success( $data ); die(); } if ( empty( $redirect ) ) { die(); } $data = compact( 'redirect' ); wp_send_json_success( $data ); die(); } /** * Maybe create an item (form, page, or view), or get the URL to the appropriate redirect. * * @param string $type supports 'form', 'page' and 'view'. * @param int $term_id * @return string redirect url to edit item. */ private static function get_new_item_redirect( $type, $term_id ) { switch ( $type ) { case 'form': return admin_url( 'admin.php?page=formidable&triggerNewFormModal=1&applicationId=' . $term_id ); case 'view': return admin_url( 'edit.php?post_type=frm_display&triggerNewViewModal=1&applicationId=' . $term_id ); case 'page': $name = FrmAppHelper::get_post_param( 'name', '', 'sanitize_text_field' ); if ( ! $name ) { return ''; } $post_id = wp_insert_post( array( 'post_type' => 'page', 'post_title' => $name, 'post_status' => 'private', ) ); FrmProApplication::add_post_to_application( $term_id, $post_id, $type ); $object_id = $post_id; return self::get_edit_url( $type, $object_id ); default: return ''; } } /** * @param string $type supports 'form', 'page' and 'view'. * @param int $term_id * @param int $item_id * @return void */ private static function add_existing_item_to_application( $type, $term_id, $item_id ) { switch ( $type ) { case 'form': self::add_missing_view_ids_to_application( $term_id, $item_id ); FrmProApplication::add_form_to_application( $term_id, $item_id ); break; case 'view': $form_id = get_post_meta( $item_id, 'frm_form_id', true ); if ( $form_id && is_numeric( $form_id ) ) { self::add_missing_form_id_to_application( $term_id, (int) $form_id ); } // Fall through to page case, we want to add post to application. case 'page': FrmProApplication::add_post_to_application( $term_id, $item_id, $type ); break; } } /** * @param int $term_id * @param int $form_id * @return void */ private static function add_missing_view_ids_to_application( $term_id, $form_id ) { global $wpdb; $where = array( 'meta_key' => 'frm_form_id', 'meta_value' => $form_id, ); $view_ids = FrmDb::get_col( $wpdb->postmeta, $where, 'post_id' ); $new_view_ids = array_diff( array_map( 'intval', $view_ids ), FrmProApplication::get_posts_for_application( $term_id, array( 'frm_display' ), array( 'fields' => 'ids' ) ) ); foreach ( $new_view_ids as $view_id ) { FrmProApplication::add_post_to_application( $term_id, $view_id, 'view' ); } } /** * @param int $term_id * @param int $form_id * @return void */ private static function add_missing_form_id_to_application( $term_id, $form_id ) { global $wpdb; $where = array( 'meta_key' => '_frm_form_id', 'meta_value' => $form_id, 'term_id' => $term_id, ); if ( FrmDb::get_var( $wpdb->termmeta, $where, 'term_id' ) ) { return; } FrmProApplication::add_form_to_application( $term_id, $form_id ); } /** * @param string $type * @param int $object_id * @return string */ private static function get_edit_url( $type, $object_id ) { switch ( $type ) { case 'form': return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $object_id ); default: return admin_url( 'post.php?post=' . $object_id . '&action=edit' ); } } /** * Remove item from application via AJAX action. * * @return void */ public static function remove_from_application() { FrmProApplicationsHelper::custom_application_permission_check(); check_ajax_referer( 'frm_ajax', 'nonce' ); $term_id = FrmAppHelper::get_post_param( 'term_id', 0, 'absint' ); if ( ! $term_id ) { die(); } $type = FrmAppHelper::get_post_param( 'type', '', 'sanitize_text_field' ); if ( ! $type || ! in_array( $type, array( 'form', 'page', 'view' ), true ) ) { die(); } $item_id = FrmAppHelper::get_post_param( 'item_id', 0, 'absint' ); if ( ! $item_id ) { die(); } switch ( $type ) { case 'form': FrmProApplication::remove_form_from_application( $term_id, $item_id ); break; case 'view': case 'page': FrmProApplication::remove_post_from_application( $term_id, $item_id, $type ); break; } $data = array(); wp_send_json_success( $data ); die(); } /** * Save application settings via AJAX action (Rename action on My Application page). * * @return void */ public static function save_application_settings() { FrmProApplicationsHelper::custom_application_permission_check(); check_ajax_referer( 'frm_ajax', 'nonce' ); $application_id = FrmAppHelper::get_post_param( 'term_id', 0, 'absint' ); if ( ! $application_id ) { die(); } $name = FrmAppHelper::get_post_param( 'name', '', 'sanitize_text_field' ); if ( ! $name ) { die(); } $args = compact( 'name' ); wp_update_term( $application_id, 'frm_application', $args ); FrmProApplication::update_timestamp( $application_id ); $data = array(); wp_send_json_success( $data ); die(); } /** * Validate application name via AJAX action. * * @return void */ public static function validate_application_name() { FrmProApplicationsHelper::custom_application_permission_check(); check_ajax_referer( 'frm_ajax', 'nonce' ); $name = FrmAppHelper::get_post_param( 'name', '', 'sanitize_text_field' ); if ( ! $name ) { die(); } // Term id may be 0 if this is a new application (via the new Application Modal, or when installing a template). $application_id = FrmAppHelper::get_post_param( 'application_id', 0, 'absint' ); $valid = ! FrmProApplication::name_is_taken( $name, $application_id ); $data = array( 'valid' => $valid ); if ( $valid ) { wp_send_json_success( $data ); } else { wp_send_json_error( $data ); } die(); } /** * Sync via AJAX action. * * @return void */ public static function sync() { FrmProApplicationsHelper::custom_application_permission_check(); check_ajax_referer( 'frm_ajax', 'nonce' ); $application_id = FrmAppHelper::get_post_param( 'application_id', 0, 'absint' ); if ( ! $application_id ) { wp_send_json_error( 'Missing required application id param' ); die(); } $summary = FrmProApplicationsHelper::sync( $application_id ); $data = compact( 'summary' ); wp_send_json_success( $data ); die(); } /** * Search via AJAX action. Used for adding forms with applications. * * @return void */ public static function search() { FrmProApplicationsHelper::custom_application_permission_check(); check_ajax_referer( 'frm_ajax', 'nonce' ); global $wpdb; $name = FrmAppHelper::get_param( 'term', '', 'get', 'sanitize_text_field' ); $terms = get_terms( array( 'taxonomy' => 'frm_application', 'hide_empty' => false, 'orderby' => 'name', 'number' => 25, 'name__like' => $name, ) ); $results = array(); foreach ( $terms as $term ) { $results[] = array( 'value' => $term->term_id, 'label' => $term->name, ); } wp_send_json( $results ); } /** * Add hook before creating a page with form or view shortcode. * * @return void */ public static function before_create_page_with_shortcode() { add_action( 'wp_insert_post', array( __CLASS__, 'after_create_page_with_shortcode' ), 10, 3 ); } /** * Add application relation after page is created with shortcode. * * @param int $post_ID * @param WP_Post $post * @param bool $update * @return void */ public static function after_create_page_with_shortcode( $post_ID, $post, $update ) { if ( $update || 'page' !== $post->post_type ) { return; } $application_id = FrmAppHelper::get_post_param( 'application_id', 0, 'absint' ); if ( ! $application_id ) { return; } FrmProApplication::add_post_to_application( $application_id, $post_ID, 'page' ); } } The Last Drown Demon - Borderlined Guy
Skip to content