Completed
Push — master ( a1aeda...5cf5b4 )
by Stephanie
02:17
created

FrmFormMigratorsHelper::dismiss_migrator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmFormMigratorsHelper {
7
8
	public static function notification_count() {
9
		$forms = self::import_links();
10
		if ( empty( $forms ) ) {
11
			return '';
12
		}
13
14
		$count = count( $forms );
15
		$dismissed = get_option( 'frm_dismissed' );
16
		if ( ! empty( $dismissed ) ) {
17
			foreach ( $forms as $form ) {
18
				if ( self::is_dismissed( $form, $dismissed ) ) {
19
					$count --;
20
				}
21
			}
22
		}
23
24
		if ( $count < 1 ) {
25
			return '';
26
		}
27
28
		return ' <span class="update-plugins"><span class="plugin-count">' . absint( $count ) . '</span></span>';
29
	}
30
31
	private static function is_dismissed( $form, $dismissed = null ) {
32
		if ( $dismissed === null ) {
33
			$dismissed = get_option( 'frm_dismissed' );
34
		}
35
36
		if ( ! empty( $dismissed ) && in_array( $form['class'], $dismissed ) ) {
37
			return true;
38
		}
39
40
		return false;
41
	}
42
43
	public static function maybe_show_download_link() {
44
		$forms = self::import_links();
45
		foreach ( $forms as $form ) {
46
			if ( ! self::is_dismissed( $form ) ) {
47
				self::install_banner( $form );
48
			} else {
49
				$label = sprintf( __( 'Import from %s', 'formidable' ), $form['name'] );
50
				self::install_button( $form, $label );
51
			}
52
		}
53
	}
54
55
	private static function import_links() {
56
		if ( ! current_user_can( 'activate_plugins' ) ) {
57
			return array();
58
		}
59
60
		$forms = array();
61
		foreach ( self::importable_forms() as $form ) {
62
			if ( class_exists( $form['class'] ) || ! is_plugin_active( $form['plugin'] ) ) {
63
				// Either the importer is installed or the source plugin isn't.
64
				continue;
65
			}
66
67
			$installer = new FrmInstallPlugin( array( 'plugin_file' => $form['importer'] ) );
68
			$form['installed'] = $installer->is_installed();
69
			$form['link']      = $installer->get_activate_link();
70
71
			$forms[] = $form;
72
		}
73
		return $forms;
74
	}
75
76
	private static function importable_forms() {
77
		return array(
78
			/*'gf' => array(
79
				'class'    => 'FrmGravityImporter',
80
				'plugin'   => 'gravityforms/gravityforms.php',
81
				'importer' => 'formidable-gravity-forms-importer/formidable-gravity-forms-importer.php',
82
				'name'     => 'Gravity Forms',
83
			),*/
84
			'pf' => array(
85
				'class'    => 'FrmPirateImporter',
86
				'plugin'   => 'pirate-forms/pirate-forms.php',
87
				'importer' => 'formidable-import-pirate-forms/pf-to-frm.php',
88
				'name'     => 'Pirate Forms',
89
			),
90
		);
91
	}
92
93
	private static function install_banner( $install ) {
94
		if ( empty( $install['link'] ) ) {
95
			return '';
96
		}
97
98
		?>
99
		<div class="frm-feature-banner">
100
			<a href="#" class="dismiss alignright" id="<?php echo esc_attr( $install['class'] ); ?>" title="<?php esc_attr_e( 'Dismiss this message', 'formidable' ); ?>">
101
				<?php FrmAppHelper::icon_by_class( 'frmfont frm_close_icon', array( 'aria-label' => 'Dismiss' ) ); ?>
102
			</a>
103
			<div class="frm-big-icon">
104
				<?php FrmAppHelper::icon_by_class( 'frmfont frm_cloud_upload_solid_icon', array( 'aria-label' => 'Import' ) ); ?>
105
			</div>
106
			<p>Did you know you can import your forms created in <?php echo esc_html( $install['name'] ); ?>?</p>
107
			<?php self::install_button( $install ); ?>
108
		</div>
109
		<?php
110
	}
111
112
	private static function install_button( $install, $label = '' ) {
113
		$primary = 'button-secondary frm-button-secondary ';
114
115
		if ( empty( $label ) ) {
116
			$label   = __( 'Get Started', 'formidable' );
117
			$primary = 'button-primary frm-button-primary ';
118
		}
119
		
120
		if ( $install['installed'] ) {
121
			?>
122
			<a rel="<?php echo esc_attr( $install['importer'] ); ?>" class="button frm-activate-addon <?php echo esc_attr( $primary . ( empty( $install['link'] ) ? 'frm_hidden' : '' ) ); ?>">
123
			<?php
124
		} else {
125
			?>
126
			<a rel="<?php echo esc_attr( $install['link'] ); ?>" class="frm-install-addon button <?php echo esc_attr( $primary ); ?>" aria-label="<?php esc_attr_e( 'Install', 'formidable' ); ?>">
127
			<?php
128
		}
129
		?>
130
		<?php echo esc_html( $label ); ?>
131
		</a>
132
		<?php
133
	}
134
135
	public static function dismiss_migrator() {
136
		check_ajax_referer( 'frm_ajax', 'nonce' );
137
		$dismissed = get_option( 'frm_dismissed' );
138
		if ( empty( $dismissed ) ) {
139
			$dismissed = array();
140
		}
141
		$dismissed[] = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
142
		update_option( 'frm_dismissed', array_filter( $dismissed ), 'no' );
143
		wp_die();
144
	}
145
}
146