Conditions | 16 |
Paths | 914 |
Total Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
20 | public function render_template( $args ) { |
||
21 | //do some work before we locate the template |
||
22 | global $current_foogallery; |
||
23 | global $current_foogallery_arguments; |
||
|
|||
24 | global $current_foogallery_template; |
||
25 | |||
26 | //set the arguments |
||
27 | $current_foogallery_arguments = $args; |
||
28 | |||
29 | //load our gallery |
||
30 | $current_foogallery = $this->find_gallery( $args ); |
||
31 | |||
32 | if ( false === $current_foogallery ) { |
||
33 | //we could not find the gallery! |
||
34 | _e( 'The gallery was not found!', 'foogallery' ); |
||
35 | return; |
||
36 | } |
||
37 | |||
38 | //check if the gallery is password protected |
||
39 | if ( post_password_required( $current_foogallery->_post ) ) { |
||
40 | echo get_the_password_form( $current_foogallery->_post ); |
||
41 | return; |
||
42 | } |
||
43 | |||
44 | //find the gallery template we will use to render the gallery |
||
45 | $current_foogallery_template = $this->get_arg( $args, 'template', $current_foogallery->gallery_template ); |
||
46 | //set a default if we have no gallery template |
||
47 | if ( empty( $current_foogallery_template ) ) { |
||
48 | $current_foogallery_template = foogallery_get_default( 'gallery_template' ); |
||
49 | } |
||
50 | |||
51 | //override the template if needed |
||
52 | if ( $current_foogallery->gallery_template !== $current_foogallery_template ) { |
||
53 | $current_foogallery->gallery_template = $current_foogallery_template; |
||
54 | } |
||
55 | |||
56 | //potentially override attachment_ids from arguments |
||
57 | $attachment_ids = $this->get_arg( $args, 'attachment_ids', false ); |
||
58 | if ( $attachment_ids ) { |
||
59 | if ( !is_array( $attachment_ids ) ) { |
||
60 | $attachment_ids = explode( ',', $attachment_ids); |
||
61 | } |
||
62 | $current_foogallery->attachment_ids = $attachment_ids; |
||
63 | } |
||
64 | |||
65 | //check if we have any attachments |
||
66 | if ( ! $current_foogallery->has_attachments() ) { |
||
67 | //no attachments! |
||
68 | do_action( "foogallery_template_no_attachments-($current_foogallery_template)", $current_foogallery ); |
||
69 | do_action( "foogallery_template_no_attachments", $current_foogallery ); |
||
70 | } else { |
||
71 | |||
72 | //create locator instance |
||
73 | $loader = $this->create_locator_instance(); |
||
74 | |||
75 | if ( false !== ( $template_location = $loader->locate_file( "gallery-{$current_foogallery_template}.php" ) ) ) { |
||
76 | |||
77 | //we have found a template! |
||
78 | do_action( 'foogallery_located_template', $current_foogallery ); |
||
79 | do_action( "foogallery_located_template-{$current_foogallery_template}", $current_foogallery ); |
||
80 | |||
81 | //try to include some JS, but allow template to opt-out based on some condition |
||
82 | if ( false !== apply_filters( "foogallery_template_load_js-{$current_foogallery_template}", true, $current_foogallery ) ) { |
||
83 | if ( false !== ( $js_location = $loader->locate_file( "gallery-{$current_foogallery_template}.js" ) ) ) { |
||
84 | $js_deps = apply_filters( "foogallery_template_js_deps-{$current_foogallery_template}", array(), $current_foogallery ); |
||
85 | $js_ver = apply_filters( "foogallery_template_js_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery ); |
||
86 | wp_enqueue_script( "foogallery-template-{$current_foogallery_template}", $js_location['url'], $js_deps, $js_ver ); |
||
87 | do_action( 'foogallery_template_enqueue_script', $current_foogallery_template, $js_location['url'] ); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | //try to include some CSS, but allow template to opt-out based on some condition |
||
92 | if ( false !== apply_filters( "foogallery_template_load_css-{$current_foogallery_template}", true, $current_foogallery ) ) { |
||
93 | if ( false !== ( $css_location = $loader->locate_file( "gallery-{$current_foogallery_template}.css" ) ) ) { |
||
94 | $css_deps = apply_filters( "foogallery_template_css_deps-{$current_foogallery_template}", array(), $current_foogallery ); |
||
95 | $css_ver = apply_filters( "foogallery_template_css_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery ); |
||
96 | foogallery_enqueue_style( "foogallery-template-{$current_foogallery_template}", $css_location['url'], $css_deps, $css_ver ); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | //finally include the actual php template! |
||
101 | if ( $template_location ) { |
||
102 | $this->load_gallery_template( $current_foogallery, $template_location['path'] ); |
||
103 | } |
||
104 | |||
105 | //cater for lightbox extensions needing to add styles and javascript |
||
106 | $lightbox = foogallery_gallery_template_setting( 'lightbox' ); |
||
107 | if ( !empty( $lightbox ) ) { |
||
108 | do_action( "foogallery_template_lightbox-{$lightbox}", $current_foogallery ); |
||
109 | } |
||
110 | |||
111 | //we have loaded all files, now let extensions do some stuff |
||
112 | do_action( "foogallery_loaded_template", $current_foogallery ); |
||
113 | do_action( "foogallery_loaded_template-($current_foogallery_template)", $current_foogallery ); |
||
114 | } else { |
||
115 | //we could not find a template! |
||
116 | _e( 'No gallery template found!', 'foogallery' ); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | if ( apply_filters( 'foogallery_render_template_clear_globals', true ) ) { |
||
121 | //cleanup globals in case there are multiple galleries on a page |
||
122 | $current_foogallery = null; |
||
123 | $current_foogallery_arguments = null; |
||
124 | $current_foogallery_template = null; |
||
125 | } |
||
126 | } |
||
127 | |||
261 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state